Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Replace update with simple delete for removeDeliveryOption #4825

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -348,20 +348,19 @@ export const sendResponsesToVault = async ({
}: {
id: string;
}): Promise<{
formRecord: FormRecord | null;
success?: boolean;
error?: string;
}> => {
try {
const { ability } = await authCheckAndThrow();

const response = await removeDeliveryOption(ability, formID);
if (!response) {
throw new Error(`Template API response was null. Request information: { ${formID} }`);
}
await removeDeliveryOption(ability, formID);

return { formRecord: response };
return {
success: true,
};
} catch (error) {
return { formRecord: null, error: (error as Error).message };
return { success: false, error: (error as Error).message };
}
};

Expand Down
6 changes: 0 additions & 6 deletions app/api/templates/[formID]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,6 @@ export const PUT = middleware(
return NextResponse.json(response);
} else if (sendResponsesToVault) {
const response = await removeDeliveryOption(ability, formID);
if (!response)
throw new Error(
`Template API response was null. Request information: method = ${
req.method
} ; query = ${JSON.stringify(props.params)} ; body = ${JSON.stringify(props.body)}`
);
return NextResponse.json(response);
}
throw new MalformedAPIRequest(
Expand Down
43 changes: 4 additions & 39 deletions lib/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1145,61 +1145,26 @@ export async function updateResponseDeliveryOption(
* @param formID The unique identifier of the form you want to modify
* @returns The updated form template or null if the record does not exist
*/
export async function removeDeliveryOption(
ability: UserAbility,
formID: string
): Promise<FormRecord | null> {
export async function removeDeliveryOption(ability: UserAbility, formID: string): Promise<void> {
try {
await authorization.canEditForm(ability, formID);

const updatedTemplate = await prisma.template
.update({
await prisma.deliveryOption
timarney marked this conversation as resolved.
Show resolved Hide resolved
.deleteMany({
where: {
id: formID,
isPublished: false,
deliveryOption: {
isNot: null,
},
},
data: {
deliveryOption: {
delete: true,
},
},
select: {
id: true,
created_at: true,
updated_at: true,
name: true,
jsonConfig: true,
isPublished: true,
deliveryOption: true,
securityAttribute: true,
formPurpose: true,
publishReason: true,
publishFormType: true,
publishDesc: true,
templateId: formID,
},
})
.catch((e) => {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
if (e.code === "P2025") {
throw new TemplateAlreadyPublishedError();
}
}
return prismaErrors(e, null);
});

if (updatedTemplate === null) return updatedTemplate;

logEvent(
ability.userID,
{ type: "Form", id: formID },
"ChangeDeliveryOption",
"Delivery Option set to the Vault"
);

return _parseTemplate(updatedTemplate);
} catch (e) {
if (e instanceof AccessControlError)
logEvent(
Expand Down
Loading