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
2 changes: 1 addition & 1 deletion src/app/api/client/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function GET(request: NextRequest) {
return errorHandler('Missing token', 422)
}
if (!clientId) {
errorHandler('Missing client Id', 422)
return errorHandler('Missing client Id', 422)
}
const copilotClient = new CopilotAPI(z.string().parse(token))
try {
Expand Down
52 changes: 35 additions & 17 deletions src/app/client-preview/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,54 @@ import { prepareCustomLabel } from '@/utils/customLabels'
export const revalidate = 0

async function getSettings(token: string) {
const { data } = await fetch(`${apiUrl}/api/settings?token=${token}`).then(
(res) => res.json(),
)
return data
try {
const { data } = await fetch(`${apiUrl}/api/settings?token=${token}`).then(
(res) => res.json(),
)
return data
} catch (error: unknown) {
console.error({ error })
throw error
}
}

async function getClient(clientId: string, token: string): Promise<IClient> {
const res = await fetch(
`${apiUrl}/api/client?clientId=${clientId}&token=${token}`,
)
if (!res.ok) {
throw new Error(`No client found with '${token}' token`)
try {
const res = await fetch(
`${apiUrl}/api/client?clientId=${clientId}&token=${token}`,
)
if (!res.ok) {
throw new Error(`No client found with '${token}' token`)
}
const { data } = await res.json()
return data
} catch (error: unknown) {
console.error({ error })
throw error
}
const { data } = await res.json()
return data
}

async function getCompany(companyId: string, token: string) {
const res = await fetch(
`${apiUrl}/api/companies?companyId=${companyId}&token=${token}`,
)
try {
const res = await fetch(
`${apiUrl}/api/companies?companyId=${companyId}&token=${token}`,
)
if (!res.ok) {
throw new Error(`No company found with '${token}' token`)
}

const { data } = await res.json()
return data
const { data } = await res.json()
return data
} catch (error: unknown) {
console.error({ error })
throw error
}
}

async function getCustomFields(token: string) {
const copilotClient = new CopilotAPI(token)
const customFieldsList = await copilotClient.getCustomFields()
return (customFieldsList.data || []) as ICustomField[]
return ((customFieldsList && customFieldsList.data) || []) as ICustomField[]
}

export default async function ClientPreviewPage({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ async function getCustomFields(token: string) {
const res = await fetch(`/api/autofill?token=${token}`, {
next: { revalidate: 0 },
})
const { autofillFields } = await res.json()
if (!res.ok) {
console.error(`No custom fields found with '${token}' token`)
return []
}

const { autofillFields } = await res.json()
return autofillFields
}

Expand Down
Loading