fix: broken endpoint to update a field in a document (#1319)

Fixes #1318 and allows to submit a partial `fieldMeta` when updating a
field instead of replacing the current `fieldMeta`, to be consistent
with other endpoints.
This commit is contained in:
Etrenak
2024-09-05 06:14:02 +02:00
committed by GitHub
parent ad4cff937d
commit 8ab7464b84
2 changed files with 35 additions and 28 deletions

View File

@ -1050,7 +1050,8 @@ export const ApiContractV1Implementation = createNextRoute(ApiContractV1, {
updateField: authenticatedMiddleware(async (args, user, team) => { updateField: authenticatedMiddleware(async (args, user, team) => {
const { id: documentId, fieldId } = args.params; const { id: documentId, fieldId } = args.params;
const { recipientId, type, pageNumber, pageWidth, pageHeight, pageX, pageY } = args.body; const { recipientId, type, pageNumber, pageWidth, pageHeight, pageX, pageY, fieldMeta } =
args.body;
const document = await getDocumentById({ const document = await getDocumentById({
id: Number(documentId), id: Number(documentId),
@ -1112,6 +1113,7 @@ export const ApiContractV1Implementation = createNextRoute(ApiContractV1, {
pageWidth, pageWidth,
pageHeight, pageHeight,
requestMetadata: extractNextApiRequestMetadata(args.req), requestMetadata: extractNextApiRequestMetadata(args.req),
fieldMeta: fieldMeta ? ZFieldMetaSchema.parse(fieldMeta) : undefined,
}); });
const remappedField = { const remappedField = {

View File

@ -65,6 +65,11 @@ export const updateField = async ({
}, },
}); });
const newFieldMeta = {
...(oldField.fieldMeta as FieldMeta),
...fieldMeta,
};
const field = prisma.$transaction(async (tx) => { const field = prisma.$transaction(async (tx) => {
const updatedField = await tx.field.update({ const updatedField = await tx.field.update({
where: { where: {
@ -78,13 +83,39 @@ export const updateField = async ({
positionY: pageY, positionY: pageY,
width: pageWidth, width: pageWidth,
height: pageHeight, height: pageHeight,
fieldMeta, fieldMeta: newFieldMeta,
}, },
include: { include: {
Recipient: true, Recipient: true,
}, },
}); });
const user = await prisma.user.findFirstOrThrow({
where: {
id: userId,
},
select: {
id: true,
name: true,
email: true,
},
});
let team: Team | null = null;
if (teamId) {
team = await prisma.team.findFirst({
where: {
id: teamId,
members: {
some: {
userId,
},
},
},
});
}
await tx.documentAuditLog.create({ await tx.documentAuditLog.create({
data: createDocumentAuditLogData({ data: createDocumentAuditLogData({
type: DOCUMENT_AUDIT_LOG_TYPE.FIELD_UPDATED, type: DOCUMENT_AUDIT_LOG_TYPE.FIELD_UPDATED,
@ -108,31 +139,5 @@ export const updateField = async ({
return updatedField; return updatedField;
}); });
const user = await prisma.user.findFirstOrThrow({
where: {
id: userId,
},
select: {
id: true,
name: true,
email: true,
},
});
let team: Team | null = null;
if (teamId) {
team = await prisma.team.findFirst({
where: {
id: teamId,
members: {
some: {
userId,
},
},
},
});
}
return field; return field;
}; };