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
30 changes: 17 additions & 13 deletions src/app/api/custom-field-access/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from 'next/server';
import { CustomFieldAccessRequestSchema } from '@/types/customFieldAccess';
import { CustomFieldAccessService } from '@/app/api/custom-field-access/services/customFieldAccess.service';
import { respondError } from '@/utils/common';
import { handleError, respondError } from '@/utils/common';
import { CopilotAPI } from '@/utils/copilotApiUtils';
import { z } from 'zod';

Expand All @@ -17,21 +17,25 @@ export async function GET(request: NextRequest) {
if (!portalId) {
return respondError('Missing portalId', 422);
}
const copilotClient = new CopilotAPI(z.string().parse(token));
const customFields = (await copilotClient.getCustomFields()).data;
try {
const copilotClient = new CopilotAPI(z.string().parse(token));
const customFields = (await copilotClient.getCustomFields()).data;

const customFieldAccessService = new CustomFieldAccessService();
const customFieldAccesses = await customFieldAccessService.findAll(z.string().parse(portalId));
const customFieldAccessService = new CustomFieldAccessService();
const customFieldAccesses = await customFieldAccessService.findAll(z.string().parse(portalId));

const customFieldsWithAccess = customFields?.map((customField) => {
const customFieldAccess = customFieldAccesses.find((access) => access.customFieldId === customField.id);
return {
...customField,
permission: customFieldAccess ? customFieldAccess.permissions : [],
};
});
const customFieldsWithAccess = customFields?.map((customField) => {
const customFieldAccess = customFieldAccesses.find((access) => access.customFieldId === customField.id);
return {
...customField,
permission: customFieldAccess ? customFieldAccess.permissions : [],
};
});

return NextResponse.json({ data: customFieldsWithAccess });
return NextResponse.json({ data: customFieldsWithAccess });
} catch (error) {
return handleError(error);
}
}

export async function PUT(request: NextRequest) {
Expand Down
3 changes: 2 additions & 1 deletion src/app/manage/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ async function getCustomFieldAccess({
const res = await fetch(`${apiUrl}/api/custom-field-access?token=${token}&portalId=${portalId}`);

if (!res.ok) {
throw new Error('Something went wrong in getCustomFieldAccess');
const body = await res.text().catch(() => '');
throw new Error(`getCustomFieldAccess failed: ${res.status} ${body.slice(0, 200)}`);
}

const { data } = await res.json();
Expand Down
3 changes: 2 additions & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ async function getCustomFieldAccess({
});

if (!res.ok) {
throw new Error('Something went wrong in getCustomFieldAccess');
const body = await res.text().catch(() => '');
throw new Error(`getCustomFieldAccess failed: ${res.status} ${body.slice(0, 200)}`);
}

const { data } = await res.json();
Expand Down
Loading