mirror of
https://github.com/documenso/documenso.git
synced 2025-11-20 03:32:14 +10:00
57 lines
1.1 KiB
TypeScript
57 lines
1.1 KiB
TypeScript
import type { Field } from '@prisma/client';
|
|
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
import { AppError, AppErrorCode } from '../../errors/app-error';
|
|
import { buildTeamWhereQuery } from '../../utils/teams';
|
|
|
|
export type GetFieldByIdOptions = {
|
|
userId: number;
|
|
teamId?: number;
|
|
fieldId: number;
|
|
documentId?: number;
|
|
templateId?: number;
|
|
};
|
|
|
|
export const getFieldById = async ({
|
|
userId,
|
|
teamId,
|
|
fieldId,
|
|
documentId,
|
|
templateId,
|
|
}: GetFieldByIdOptions) => {
|
|
let field: Field | null = null;
|
|
|
|
if (documentId) {
|
|
field = await prisma.field.findFirst({
|
|
where: {
|
|
id: fieldId,
|
|
document: {
|
|
id: documentId,
|
|
team: buildTeamWhereQuery(teamId, userId),
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
if (templateId) {
|
|
field = await prisma.field.findFirst({
|
|
where: {
|
|
id: fieldId,
|
|
template: {
|
|
id: templateId,
|
|
team: buildTeamWhereQuery(teamId, userId),
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
if (!field) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: 'Field not found',
|
|
});
|
|
}
|
|
|
|
return field;
|
|
};
|