-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62168e0
commit 290ccf2
Showing
10 changed files
with
320 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 49 additions & 29 deletions
78
core/app/[locale]/(default)/gift-certificates/_actions/lookup-balance.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,75 @@ | ||
'use server' | ||
'use server'; | ||
|
||
import { getTranslations } from 'next-intl/server'; | ||
import { z } from 'zod'; | ||
|
||
export async function lookupGiftCertificateBalance(code: string) { | ||
const giftCertificateSchema = z.object({ | ||
code: z.string(), | ||
balance: z.string(), | ||
currency_code: z.string(), | ||
}); | ||
|
||
interface SuccessResponse { | ||
balance: number; | ||
currencyCode: string; | ||
} | ||
|
||
interface ErrorResponse { | ||
error: string; | ||
details?: unknown; | ||
} | ||
|
||
type LookupResponse = SuccessResponse | ErrorResponse; | ||
|
||
export async function lookupGiftCertificateBalance(code: string): Promise<LookupResponse> { | ||
const t = await getTranslations('GiftCertificate.Actions.Lookup'); | ||
|
||
if (!code) { | ||
return { error: t('noCode') } | ||
return { error: t('noCode') }; | ||
} | ||
|
||
const apiUrl = `https://api.bigcommerce.com/stores/${process.env.BIGCOMMERCE_STORE_HASH}/v2/gift_certificates` | ||
const apiUrl = `https://api.bigcommerce.com/stores/${process.env.BIGCOMMERCE_STORE_HASH}/v2/gift_certificates`; | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
'X-Auth-Token': process.env.GIFT_CERTIFICATE_V3_API_TOKEN ?? '', | ||
'Accept': 'application/json' | ||
} | ||
Accept: 'application/json', | ||
}; | ||
|
||
try { | ||
const response = await fetch(`${apiUrl}?limit=1&code=${encodeURIComponent(code)}`, { | ||
method: 'GET', | ||
headers: headers | ||
}) | ||
headers, | ||
}); | ||
|
||
if (response.status === 404 || response.status === 204) { | ||
return { error: t('notFound') } | ||
return { error: t('notFound') }; | ||
} | ||
|
||
if (!response.ok) { | ||
console.error(`v2 Gift Certificate API responded with status ${response.status}: ${response.statusText}`) | ||
return { error: t('error') } | ||
return { error: t('error') }; | ||
} | ||
|
||
const parseResult = z.array(giftCertificateSchema).safeParse(await response.json()); | ||
|
||
if (!parseResult.success) { | ||
return { error: t('error') }; | ||
} | ||
|
||
const data = await response.json() | ||
|
||
if (Array.isArray(data) && data.length > 0 && typeof data[0].balance !== 'undefined') { | ||
// There isn't a way to query the exact code in the v2 Gift Certificate API, | ||
// so we'll loop through the results to make sure it's not a partial match | ||
for (const certificate of data) { | ||
if (certificate.code === code) { | ||
return { balance: parseFloat(data[0].balance), currencyCode: data[0].currency_code } | ||
} | ||
} | ||
|
||
// No exact match, so consider it not found | ||
return { error: t('notFound') } | ||
} else { | ||
console.error('Unexpected v2 Gift Certificate API response structure') | ||
return { error: t('error') } | ||
const data = parseResult.data; | ||
const certificate = data.find((cert) => cert.code === code); | ||
|
||
if (!certificate) { | ||
return { error: t('notFound') }; | ||
} | ||
|
||
return { | ||
balance: parseFloat(certificate.balance), | ||
currencyCode: certificate.currency_code, | ||
}; | ||
} catch (error) { | ||
console.error('Error checking gift certificate balance:', error) | ||
return { error: t('error') } | ||
return { | ||
error: t('error'), | ||
details: error, | ||
}; | ||
} | ||
} |
Oops, something went wrong.