Skip to content

Commit 49dce79

Browse files
committed
feat: improves reviews.create to handle authentication
1 parent a8da62c commit 49dce79

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

packages/api/src/platforms/vtex/clients/commerce/index.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import type { SalesChannel } from './types/SalesChannel'
1919
import type { MasterDataResponse } from './types/Newsletter'
2020
import type { Address, AddressInput } from './types/Address'
2121
import type { DeliveryMode, SelectedAddress } from './types/ShippingData'
22-
import { getStoreCookie, getWithCookie } from '../../utils/cookies'
22+
import {
23+
getCookieFromRequestHeaders,
24+
getStoreCookie,
25+
getWithCookie,
26+
} from '../../utils/cookies'
2327
import type { ProductRating } from './types/ProductRating'
2428
import type {
2529
CreateProductReviewInput,
@@ -28,6 +32,7 @@ import type {
2832
} from './types/ProductReview'
2933
import { adaptObject } from '../../utils/adaptObject'
3034
import { camelToSnakeCase } from '../../utils/camelToSnakeCase'
35+
import { NotAuthorizedError } from '../../../errors'
3136

3237
type ValueOf<T> = T extends Record<string, infer K> ? K : never
3338

@@ -383,10 +388,22 @@ export const VtexCommerce = (
383388
},
384389
reviews: {
385390
create: (input: CreateProductReviewInput): Promise<string> => {
391+
const authCookieKey: string = `VtexIdclientAutCookie_${account}`
392+
393+
const authCookie = getCookieFromRequestHeaders(ctx, authCookieKey) ?? ''
394+
395+
if (!authCookie) {
396+
throw new NotAuthorizedError('Missing auth cookie')
397+
}
398+
386399
return fetchAPI(
387400
`${base}/${REVIEWS_AND_RATINGS_API_PATH}/review`,
388401
{
389402
...BASE_INIT,
403+
headers: {
404+
...BASE_INIT.headers,
405+
VtexIdclientAutCookie: authCookie,
406+
},
390407
body: JSON.stringify(input),
391408
method: 'POST',
392409
},

packages/api/src/platforms/vtex/resolvers/createProductReview.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const createProductReview = async (
66
{ data }: MutationCreateProductReviewArgs,
77
{ clients: { commerce } }: Context
88
): Promise<string> => {
9-
return commerce.reviews.create({
9+
return await commerce.reviews.create({
1010
...data,
1111
approved: true,
1212
})

0 commit comments

Comments
 (0)