diff --git a/src/server/actions/Agencies.ts b/src/server/actions/Agencies.ts index f0f3b4d..cd7ea1c 100644 --- a/src/server/actions/Agencies.ts +++ b/src/server/actions/Agencies.ts @@ -375,6 +375,37 @@ export async function approveAgency( const agency = await AgencyModel.findById(id).exec(); return agency as Agency; } + +/** + * + * @param id The ID of the agencyInfoForm to reject + * @returns The updated agency + * @throws 404 if no agency is found with the ID + */ +export async function rejectAgency(id: string): Promise { + await authenticateServerAction(); + await dbConnect(); + + // Set the latency agencyInfoForm to rejected + const updatedAgency = await AgencyInfoFormModel.updateOne( + { _id: id }, + { + $set: { approved: false }, + } + ).catch((error) => { + mongoErrorHandler(error); + }); + + if (updatedAgency!.modifiedCount === 0) { + throw new JSendResponse({ + status: 'fail', + data: { message: 'AgencyInfoForm not found' }, + }); + } + + return updatedAgency as unknown as AgencyInfoForm; +} + /** * Handles common MongoDB insertion errors and throws an appropriate ApiError. * @param error - The error object returned by MongoDB. diff --git a/src/server/models/Agency.ts b/src/server/models/Agency.ts index f470e39..1e7092e 100644 --- a/src/server/models/Agency.ts +++ b/src/server/models/Agency.ts @@ -67,18 +67,19 @@ AgencySchema.virtual('currentStatus').get(function (this: Agency) { } } - const differenceInMilliseconds: number = - currentTime.getTime() - this.updatedAt.getTime(); - const differenceInDays: number = Math.floor( - differenceInMilliseconds / (1000 * 3600 * 24) - ); - if (differenceInDays < this.updateScheduleInDays - 14) { - return agencyUpdateStatus.Completed; - } else if (differenceInDays < this.updateScheduleInDays) { - return agencyUpdateStatus.NeedsReview; - } else if (differenceInDays >= this.updateScheduleInDays) { - return agencyUpdateStatus.Expired; + // Check latest form for approval + if (this.info.length !== 0) { + return this.info[this.info.length - 1].approved + ? agencyUpdateStatus.Completed + : agencyUpdateStatus.Expired; + } + + // If there is no info associated with an agency look at the outer status + if (this.approvalStatus) { + return this.approvalStatus; } + + return agencyUpdateStatus.Expired; }); AgencySchema.virtual('daysSinceEmailSent').get(function () { diff --git a/src/utils/conversions.ts b/src/utils/conversions.ts index aef8db9..ab3ae68 100644 --- a/src/utils/conversions.ts +++ b/src/utils/conversions.ts @@ -155,6 +155,7 @@ function zodLanguagesToTs(data: Inputs): AgencyInfoForm['languages'] { export function zodFormToTs(data: Inputs): AgencyInfoForm { const agencyInfo: AgencyInfoForm = { + approved: false, legalAgencyName: data.legalName, alsoKnownAs: data.akas.split(' '), legalOrganizationalStatus: data.legalStatus.split(' '), diff --git a/src/utils/types/models.ts b/src/utils/types/models.ts index 2d494b5..4e99956 100644 --- a/src/utils/types/models.ts +++ b/src/utils/types/models.ts @@ -39,6 +39,7 @@ export interface AgencyInfoForm { _id?: string; createdAt?: Date; updatedAt?: Date; + approved: boolean; legalAgencyName: string; alsoKnownAs?: string[]; legalOrganizationalStatus: ( diff --git a/test/testData/testAgency.ts b/test/testData/testAgency.ts index b9c21ca..e347447 100644 --- a/test/testData/testAgency.ts +++ b/test/testData/testAgency.ts @@ -3,6 +3,7 @@ export const testAgency: Agency = { name: 'This is another agency', info: [ { + approved: false, legalAgencyName: 'This is another agency', alsoKnownAs: [], legalOrganizationalStatus: ['Non-profit'],