Skip to content

Commit

Permalink
Fix registration types & cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy-Gonzalez committed Oct 23, 2024
1 parent 1f00d43 commit bb8bca4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 140 deletions.
6 changes: 3 additions & 3 deletions src/services/admission/admission-router.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import Models from "../../database/models";
import { DecisionStatus, DecisionResponse, AdmissionDecision } from "./admission-schemas";
import { RegistrationFormat } from "../registration/registration-formats";
import { RegistrationTemplates } from "../../common/config";
import { Gender, Degree, Race, HackInterest, HackOutreach } from "../registration/registration-schemas";
import { Gender, Degree, Race, HackInterest, HackOutreach, RegistrationApplication } from "../registration/registration-schemas";
import { getAsStaff, getAsUser, putAsStaff, putAsUser, getAsAttendee, putAsApplicant, TESTER } from "../../common/testTools";
import { StatusCode } from "status-code-enum";
import type * as MailLib from "../../services/mail/mail-lib";
Expand Down Expand Up @@ -48,7 +47,8 @@ const TESTER_APPLICATION = {
race: [Race.NO_ANSWER],
hackInterest: [HackInterest.TECHNICAL_WORKSHOPS],
hackOutreach: [HackOutreach.INSTAGRAM],
} satisfies RegistrationFormat;
hasSubmitted: false,
} satisfies RegistrationApplication;

const updateRequest = [
{
Expand Down
13 changes: 6 additions & 7 deletions src/services/admission/admission-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import {
DecisionRequestSchema,
DecisionAlreadyRSVPdError,
DecisionAlreadyRSVPdErrorSchema,
ApplicationNotFoundError,
DecisionNotFoundError,
DecisionNotFoundErrorSchema,
ApplicationNotFoundErrorSchema,
AdmissionDecisionSchema,
} from "./admission-schemas";
import Models from "../../database/models";
Expand All @@ -25,6 +23,7 @@ import { sendMail } from "../mail/mail-lib";
import specification, { Tag } from "../../middleware/specification";
import { z } from "zod";
import { SuccessResponseSchema, UserIdSchema } from "../../common/schemas";
import { RegistrationNotFoundError, RegistrationNotFoundErrorSchema } from "../registration/registration-schemas";

const admissionRouter = Router();

Expand Down Expand Up @@ -72,9 +71,9 @@ admissionRouter.put(
schema: DecisionNotFoundErrorSchema,
},
{
id: ApplicationNotFoundError.error,
id: RegistrationNotFoundError.error,
description: "Couldn't find user's application",
schema: ApplicationNotFoundErrorSchema,
schema: RegistrationNotFoundErrorSchema,
},
],
[StatusCode.ClientErrorForbidden]: {
Expand All @@ -99,7 +98,7 @@ admissionRouter.put(
// Verify they have an application
const application = await Models.RegistrationApplication.findOne({ userId });
if (!application) {
return res.status(StatusCode.ClientErrorNotFound).send(ApplicationNotFoundError);
return res.status(StatusCode.ClientErrorNotFound).send(RegistrationNotFoundError);
}

// Must be accepted to make a decision
Expand Down Expand Up @@ -171,7 +170,7 @@ admissionRouter.put(
},
[StatusCode.ClientErrorNotFound]: {
description: "A applicant's application was not found",
schema: ApplicationNotFoundErrorSchema,
schema: RegistrationNotFoundErrorSchema,
},
},
}),
Expand All @@ -189,7 +188,7 @@ admissionRouter.put(
if (existingDecision?.status === DecisionStatus.TBD && status !== DecisionStatus.TBD) {
const application = await Models.RegistrationApplication.findOne({ userId: existingDecision.userId });
if (!application) {
return res.status(StatusCode.ClientErrorNotFound).send(ApplicationNotFoundError);
return res.status(StatusCode.ClientErrorNotFound).send(RegistrationNotFoundError);
}
recipients.push(application.emailAddress);
}
Expand Down
5 changes: 0 additions & 5 deletions src/services/admission/admission-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,3 @@ export const [DecisionNotFoundError, DecisionNotFoundErrorSchema] = CreateErrorA
error: "DecisionNotFound",
message: "Couldn't find your decision!",
});

export const [ApplicationNotFoundError, ApplicationNotFoundErrorSchema] = CreateErrorAndSchema({
error: "ApplicationNotFound",
message: "Couldn't find your application!",
});
112 changes: 0 additions & 112 deletions src/services/registration/registration-formats.ts

This file was deleted.

22 changes: 14 additions & 8 deletions src/services/registration/registration-router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ import { StatusCode } from "status-code-enum";
import Models from "../../database/models";
import { RegistrationTemplates } from "../../common/config";
import { TESTER, getAsUser, getAsAdmin, postAsUser } from "../../common/testTools";
import { RegistrationFormat } from "./registration-formats";
import { Degree, Gender, HackInterest, HackOutreach, Race, RegistrationApplication } from "./registration-schemas";
import {
Degree,
Gender,
HackInterest,
HackOutreach,
Race,
RegistrationApplication,
RegistrationApplicationRequest,
} from "./registration-schemas";
import type * as MailLib from "../../services/mail/mail-lib";
import type { AxiosResponse } from "axios";
import { MailInfo } from "../mail/mail-schemas";

const APPLICATION = {
isProApplicant: false,
userId: TESTER.id,
preferredName: TESTER.name,
legalName: TESTER.name,
emailAddress: TESTER.email,
Expand All @@ -29,14 +35,14 @@ const APPLICATION = {
race: [Race.NO_ANSWER],
hackInterest: [HackInterest.TECHNICAL_WORKSHOPS],
hackOutreach: [HackOutreach.INSTAGRAM],
} satisfies RegistrationFormat;
} satisfies RegistrationApplicationRequest;

const UNSUBMITTED_REGISTRATION = { hasSubmitted: false, ...APPLICATION } satisfies RegistrationApplication;
const UNSUBMITTED_REGISTRATION = { userId: TESTER.id, hasSubmitted: false, ...APPLICATION } satisfies RegistrationApplication;
const UNSUBMITTED_OTHER_REGISTRATION = {
...UNSUBMITTED_REGISTRATION,
userId: "otherUser",
} satisfies RegistrationApplication;
const SUBMITTED_REGISTRATION = { hasSubmitted: true, ...APPLICATION } satisfies RegistrationApplication;
const SUBMITTED_REGISTRATION = { userId: TESTER.id, hasSubmitted: true, ...APPLICATION } satisfies RegistrationApplication;

describe("GET /registration/", () => {
beforeEach(async () => {
Expand Down Expand Up @@ -83,7 +89,7 @@ describe("GET /registration/userid/:USERID", () => {
describe("POST /registration/", () => {
it("should create registration", async () => {
const response = await postAsUser("/registration/").send(APPLICATION).expect(StatusCode.SuccessOK);
expect(JSON.parse(response.text)).toMatchObject(UNSUBMITTED_REGISTRATION);
expect(JSON.parse(response.text)).toMatchObject(APPLICATION);

// Stored in DB
const stored: RegistrationApplication | null = await Models.RegistrationApplication.findOne({
Expand All @@ -98,7 +104,7 @@ describe("POST /registration/", () => {
degree: "PhD of Data Corruption",
});
const response = await postAsUser("/registration/").send(APPLICATION).expect(StatusCode.SuccessOK);
expect(JSON.parse(response.text)).toMatchObject(UNSUBMITTED_REGISTRATION);
expect(JSON.parse(response.text)).toMatchObject(APPLICATION);

// Stored in DB
const stored: RegistrationApplication | null = await Models.RegistrationApplication.findOne({
Expand Down
20 changes: 15 additions & 5 deletions src/services/registration/registration-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Models from "../../database/models";
import {
RegistrationAlreadySubmittedError,
RegistrationAlreadySubmittedErrorSchema,
RegistrationApplication,
RegistrationApplicationRequestSchema,
RegistrationApplicationSchema,
RegistrationClosedError,
Expand Down Expand Up @@ -141,17 +142,26 @@ registrationRouter.post(
async (req, res) => {
const { id: userId } = getAuthenticatedUser(req);

const registrationData = req.body;
const setRequest = req.body;

const registrationInfo = await Models.RegistrationApplication.findOne({ userId: userId });
if (registrationInfo?.hasSubmitted ?? false) {
return res.status(StatusCode.ClientErrorBadRequest).send(RegistrationAlreadySubmittedError);
}

const newRegistrationInfo = await Models.RegistrationApplication.findOneAndReplace({ userId: userId }, registrationData, {
upsert: true,
new: true,
});
const updateRegistration: RegistrationApplication = {
...setRequest,
userId,
hasSubmitted: false,
};
const newRegistrationInfo = await Models.RegistrationApplication.findOneAndReplace(
{ userId: userId },
updateRegistration,
{
upsert: true,
new: true,
},
);
if (!newRegistrationInfo) {
throw Error("Failed to update registration info");
}
Expand Down

0 comments on commit bb8bca4

Please sign in to comment.