diff --git a/app/(gcforms)/[locale]/(form administration)/form-builder/actions.ts b/app/(gcforms)/[locale]/(form administration)/form-builder/actions.ts index 21103652f4..065a2c4480 100644 --- a/app/(gcforms)/[locale]/(form administration)/form-builder/actions.ts +++ b/app/(gcforms)/[locale]/(form administration)/form-builder/actions.ts @@ -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 }; } }; diff --git a/app/api/templates/[formID]/route.ts b/app/api/templates/[formID]/route.ts index 48760a41a3..0c7714567b 100644 --- a/app/api/templates/[formID]/route.ts +++ b/app/api/templates/[formID]/route.ts @@ -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( diff --git a/lib/templates.ts b/lib/templates.ts index 482d972e3f..7f0b46019e 100644 --- a/lib/templates.ts +++ b/lib/templates.ts @@ -1143,54 +1143,31 @@ export async function updateResponseDeliveryOption( /** * Remove DeliveryOption from template. Form responses will be sent to the Vault. * @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 + * @returns void */ -export async function removeDeliveryOption( - ability: UserAbility, - formID: string -): Promise { +export async function removeDeliveryOption(ability: UserAbility, formID: string): Promise { try { await authorization.canEditForm(ability, formID); - const updatedTemplate = await prisma.template - .update({ - 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, - }, - }) - .catch((e) => { - if (e instanceof Prisma.PrismaClientKnownRequestError) { - if (e.code === "P2025") { - throw new TemplateAlreadyPublishedError(); - } - } - return prismaErrors(e, null); - }); + // Don't change delivery option if the form is published + const template = await prisma.template.findFirstOrThrow({ + where: { + id: formID, + }, + select: { + isPublished: true, + }, + }); - if (updatedTemplate === null) return updatedTemplate; + if (!template) throw new TemplateNotFoundError(); + + if (template.isPublished) throw new TemplateAlreadyPublishedError(); + + await prisma.deliveryOption.deleteMany({ + where: { + templateId: formID, + }, + }); logEvent( ability.userID, @@ -1198,8 +1175,6 @@ export async function removeDeliveryOption( "ChangeDeliveryOption", "Delivery Option set to the Vault" ); - - return _parseTemplate(updatedTemplate); } catch (e) { if (e instanceof AccessControlError) logEvent( diff --git a/lib/tests/templates.test.ts b/lib/tests/templates.test.ts index 9341303725..3e9701d4ea 100644 --- a/lib/tests/templates.test.ts +++ b/lib/tests/templates.test.ts @@ -522,37 +522,20 @@ describe("Template CRUD functions", () => { }); it("Remove DeliveryOption from template", async () => { - (prismaMock.template.update as jest.MockedFunction).mockResolvedValue( - buildPrismaResponse("formtestID", formConfiguration) - ); + (prismaMock.template.findFirstOrThrow as jest.MockedFunction).mockResolvedValue({ + isPublished: false, + }); - const updatedTemplate = await removeDeliveryOption(user1Ability, "formtestID"); + await removeDeliveryOption(user1Ability, "formtestID"); - expect(prismaMock.template.update).toHaveBeenCalledWith( + expect(prismaMock.deliveryOption.deleteMany).toHaveBeenCalledWith( expect.objectContaining({ where: { - id: "formtestID", - isPublished: false, - deliveryOption: { - isNot: null, - }, - }, - data: { - deliveryOption: { - delete: true, - }, + templateId: "formtestID", }, }) ); - expect(updatedTemplate).toEqual( - expect.objectContaining({ - id: "formtestID", - form: formConfiguration, - isPublished: false, - securityAttribute: "Unclassified", - }) - ); expect(mockedLogEvent).toHaveBeenCalledWith( user1Ability.userID, { id: "formtestID", type: "Form" },