Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export const createCustomerProfile = async ({
: buildErrorResponse(createCustomerResponse)
}

export const createWholesaleProfile = ({
export const createWholesaleProfile = async ({
email,
sessionToken,
wholesale,
Expand Down
125 changes: 95 additions & 30 deletions apps/herbatika/src/components/reviews/product-review-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,43 @@ const GENERIC_REVIEW_SUBMIT_ERROR =
"Recenziu sa nepodarilo odoslať. Skúste to prosím znova."
const PURCHASE_REQUIRED_REVIEW_ERROR =
"Na napísanie recenzie musíte mať tento produkt zakúpený."
const REVIEW_VALIDATION_ERROR =
"Skontrolujte prosím hodnotenie a text recenzie."
const BAD_REQUEST_REVIEW_STATUSES = new Set([400, 422])
// Broad duplicate keywords are skipped for validation statuses below.
const BROAD_DUPLICATE_REVIEW_MESSAGE_PATTERNS = [
"already",
"duplicate",
"exist",
"reviewed",
] as const
const DUPLICATE_REVIEW_MESSAGE_RULES = [
["already", "review"],
["already", "rated"],
["already", "submitted"],
["already", "exists"],
["duplicate"],
["review", "exists"],
["reviewed", "product"],
] as const
const REVIEW_VALIDATION_MESSAGE_RULES = [
{
patterns: ["rating"],
message: "Vyberte prosím hodnotenie.",
},
{
patterns: ["content"],
message: "Napíšte prosím text recenzie.",
},
{
patterns: ["text"],
message: "Napíšte prosím text recenzie.",
},
{
patterns: ["title"],
message: "Skontrolujte prosím nadpis recenzie.",
},
] as const

const hasErrorShape = (
error: unknown
Expand Down Expand Up @@ -53,20 +90,53 @@ const isPurchaseRequiredReviewMessage = (normalizedMessage: string) => {
)
}

const isSpecificDuplicateReviewMessage = (normalizedMessage: string) =>
DUPLICATE_REVIEW_MESSAGE_RULES.some((patterns) =>
patterns.every((pattern) => normalizedMessage.includes(pattern))
)

const isBroadDuplicateReviewMessage = (normalizedMessage: string) =>
BROAD_DUPLICATE_REVIEW_MESSAGE_PATTERNS.some((pattern) =>
normalizedMessage.includes(pattern)
)

const isDuplicateReviewError = (
Comment thread
BleedingDev marked this conversation as resolved.
status: number | undefined,
normalizedMessage: string
) =>
status === 409 ||
normalizedMessage.includes("already") ||
normalizedMessage.includes("duplicate") ||
normalizedMessage.includes("exist") ||
normalizedMessage.includes("reviewed")

const resolveStatusReviewMessage = (
status: number | undefined,
message: string
) => {
if (status === 409 || isSpecificDuplicateReviewMessage(normalizedMessage)) {
return true
}

if (status && BAD_REQUEST_REVIEW_STATUSES.has(status)) {
return false
}

return isBroadDuplicateReviewMessage(normalizedMessage)
Comment thread
BleedingDev marked this conversation as resolved.
}

// Multi-pattern validation rules intentionally use AND semantics.
const resolveReviewValidationMessage = (normalizedMessage: string) =>
REVIEW_VALIDATION_MESSAGE_RULES.find(({ patterns }) =>
patterns.every((pattern) => normalizedMessage.includes(pattern))
)?.message ?? REVIEW_VALIDATION_ERROR

const resolveKnownReviewErrorMessage = ({
normalizedMessage,
status,
}: {
normalizedMessage: string
status: number | undefined
}) => {
const tokenMessage = resolveTokenMessage(normalizedMessage)
if (tokenMessage) {
return tokenMessage
}

if (status === 409) {
return "Tento produkt ste už hodnotili."
}

if (status === 401) {
Comment thread
BleedingDev marked this conversation as resolved.
return "Pre odoslanie recenzie sa prosím prihláste."
}
Expand All @@ -75,8 +145,16 @@ const resolveStatusReviewMessage = (
return "Recenziu pre tento produkt momentálne nemôžete odoslať."
}

if (status === 400 || status === 422) {
return message || "Skontrolujte prosím hodnotenie a text recenzie."
if (isPurchaseRequiredReviewMessage(normalizedMessage)) {
return PURCHASE_REQUIRED_REVIEW_ERROR
}

if (isDuplicateReviewError(status, normalizedMessage)) {
return "Tento produkt ste už hodnotili."
}

if (status && BAD_REQUEST_REVIEW_STATUSES.has(status)) {
return resolveReviewValidationMessage(normalizedMessage)
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

if (status && status >= 500) {
Expand All @@ -98,23 +176,10 @@ export const resolveProductReviewSubmitErrorMessage = (error: unknown) => {
return GENERIC_REVIEW_SUBMIT_ERROR
}

const tokenMessage = resolveTokenMessage(normalizedMessage)
if (tokenMessage) {
return tokenMessage
}

if (isDuplicateReviewError(status, normalizedMessage)) {
return "Tento produkt ste už hodnotili."
}

if (isPurchaseRequiredReviewMessage(normalizedMessage)) {
return PURCHASE_REQUIRED_REVIEW_ERROR
}

const statusMessage = resolveStatusReviewMessage(status, message)
if (statusMessage) {
return statusMessage
}
const knownMessage = resolveKnownReviewErrorMessage({
normalizedMessage,
status,
})

return message || GENERIC_REVIEW_SUBMIT_ERROR
return knownMessage || message || GENERIC_REVIEW_SUBMIT_ERROR
}
Loading