All files / react-app/src/formSchemas temporary-extension.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 5/5
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91              23x                     163x       163x               163x 163x   73x   92x                           63x                                                                   1x 1x            
import { events } from "shared-types/events";
import type { TemporaryExtensionSchema } from "shared-types/events/temporary-extension";
import { z } from "zod";
 
import { getItem, idIsApproved, itemExists } from "@/api";
import { isAuthorizedState } from "@/utils";
 
export const formSchema = events["temporary-extension"].baseSchema
  .omit({
    id: true,
    waiverNumber: true,
    authority: true,
  })
  .extend({
    ids: z.object({
      validAuthority: z
        .object({
          waiverNumber: events["temporary-extension"].baseSchema.shape.waiverNumber
            .refine(async (value) => await itemExists(value), {
              message:
                "According to our records, this Approved Initial or Renewal Waiver Number does not yet exist. Please check the Approved Initial or Renewal Waiver Number and try entering it again.",
            })
            .refine(async (value) => idIsApproved(value), {
              message:
                "According to our records, this Approved Initial or Renewal Waiver Number is not approved. You must supply an approved Initial or Renewal Waiver Number.",
            }),
          authority: events["temporary-extension"].baseSchema.shape.authority,
        })
        .refine(
          async (data) => {
            try {
              const originalWaiverData = await getItem(data.waiverNumber);
 
              return !(originalWaiverData._source.authority !== data.authority);
            } catch {
              return z.never;
            }
          },
          {
            message:
              "The selected Temporary Extension Type does not match the Approved Initial or Renewal Waiver's type.",
            path: ["authority"],
          },
        ),
      id: events["temporary-extension"].baseSchema.shape.id
        .refine(isAuthorizedState, {
          message:
            "You can only submit for a state you have access to. If you need to add another state, visit your IDM user profile to request access.",
        })
        .refine(async (value) => !(await itemExists(value)), {
          message:
            "According to our records, this Temporary Extension Request Number already exists. Please check the Temporary Extension Request Number and try entering it again.",
        }),
    }),
    // .superRefine(async (data, ctx) => {
    //   try {
    //     // Check that the original waiver and temp extension have the same id up to the last period
    //     const waiverNumberPrefix = data.validAuthority.waiverNumber.substring(
    //       0,
    //       data.validAuthority.waiverNumber.lastIndexOf("."),
    //     );
    //     const idPrefix = data.id.substring(0, data.id.lastIndexOf("."));
    //     if (waiverNumberPrefix !== idPrefix) {
    //       ctx.addIssue({
    //         message:
    //           "The Approved Initial or Renewal Waiver Number and the Temporary Extension Request Number must be identical until the last period.",
    //         code: z.ZodIssueCode.custom,
    //         fatal: false,
    //         path: ["id"],
    //       });
    //     }
    //     return z.never;
    //   } catch (error) {
    //     // If we've failed here, the item does not exist, and the originalWaiverNumberSchema validation will throw the correct errors.
    //     console.error(error);
    //     return z.never;
    //   }
    // }),
  })
  .transform((data) => {
    const {
      ids: { id, validAuthority },
      ...restOfForm
    } = data;
    return {
      ...restOfForm,
      ...validAuthority,
      id,
    } satisfies TemporaryExtensionSchema;
  });