Skip to content

Commit

Permalink
Use new CreateErrorAndSchema helper for all errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy-Gonzalez committed Oct 20, 2024
1 parent a2b3e9d commit 4b34cbc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 63 deletions.
10 changes: 3 additions & 7 deletions src/services/auth/auth-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { prop } from "@typegoose/typegoose";
import { z } from "zod";
import { Device } from "../../common/config";
import { UserIdSchema } from "../user/user-schemas";
import { CreateErrorAndSchema } from "../../common/schemas";

export class AuthInfo {
@prop({ required: true })
Expand Down Expand Up @@ -91,12 +92,7 @@ export const RefreshTokenSchema = z
})
.openapi("RefreshToken");

export const AuthorizationFailedErrorSchema = z.object({
error: z.literal("AuthorizationFailed"),
message: z.literal("Failed to authenticate"),
});

export const AuthorizationFailedError: z.infer<typeof AuthorizationFailedErrorSchema> = {
export const [AuthorizationFailedError, AuthorizationFailedErrorSchema] = CreateErrorAndSchema({
error: "AuthorizationFailed",
message: "Failed to authenticate",
};
});
24 changes: 5 additions & 19 deletions src/services/shop/shop-schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { prop } from "@typegoose/typegoose";
import { z } from "zod";
import { CreateErrorAndSchema } from "../../common/schemas";

export class ShopItem {
@prop({ required: true })
Expand Down Expand Up @@ -111,32 +112,17 @@ export const ShopItemBuyRequestSchema = z.object({
instance: z.string().openapi({ example: "1x3" }),
});

export const ShopItemAlreadyExistsErrorSchema = z.object({
error: z.literal("AlreadyExists"),
message: z.literal("An item with that id already exists, did you mean to update it instead?"),
});

export const ShopItemAlreadyExistsError: z.infer<typeof ShopItemAlreadyExistsErrorSchema> = {
export const [ShopItemAlreadyExistsError, ShopItemAlreadyExistsErrorSchema] = CreateErrorAndSchema({
error: "AlreadyExists",
message: "An item with that id already exists, did you mean to update it instead?",
};

export const ShopItemNotFoundErrorSchema = z.object({
error: z.literal("NotFound"),
message: z.literal("Failed to find item"),
});

export const ShopItemNotFoundError: z.infer<typeof ShopItemNotFoundErrorSchema> = {
export const [ShopItemNotFoundError, ShopItemNotFoundErrorSchema] = CreateErrorAndSchema({
error: "NotFound",
message: "Failed to find item",
};

export const ShopInsufficientFundsErrorSchema = z.object({
error: z.literal("InsufficientFunds"),
message: z.literal("You don't have enough to purchase that item!"),
});

export const ShopInsufficientFundsError: z.infer<typeof ShopInsufficientFundsErrorSchema> = {
export const [ShopInsufficientFundsError, ShopInsufficientFundsErrorSchema] = CreateErrorAndSchema({
error: "InsufficientFunds",
message: "You don't have enough to purchase that item!",
};
});
11 changes: 3 additions & 8 deletions src/services/staff/staff-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RouterError } from "../../middleware/error-handler";
import { AttendeeProfile } from "../../database/attendee-db";
import { UserIdSchema } from "../user/user-schemas";
import { z } from "zod";
import { SuccessResponseSchema } from "../../common/schemas";
import { CreateErrorAndSchema, SuccessResponseSchema } from "../../common/schemas";

export class StaffShift {
@prop({ required: true })
Expand Down Expand Up @@ -60,12 +60,7 @@ export const ShiftsAddRequestSchema = z.object({
shifts: z.array(z.string()).openapi({ example: ["event1"] }),
});

export const CodeExpiredErrorSchema = z.object({
error: z.literal("CodeExpired"),
message: z.literal("The code for this event has expired"),
});

export const CodeExpiredError: z.infer<typeof CodeExpiredErrorSchema> = {
export const [CodeExpiredError, CodeExpiredErrorSchema] = CreateErrorAndSchema({
error: "CodeExpired",
message: "The code for this event has expired",
};
});
44 changes: 15 additions & 29 deletions src/services/user/user-schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { prop } from "@typegoose/typegoose";
import { z } from "zod";
import { CreateErrorAndSchema } from "../../common/schemas";

export class UserInfo {
@prop({ required: true })
Expand Down Expand Up @@ -61,46 +62,31 @@ export const ScanEventRequestSchema = z
})
.openapi("ScanEventRequest");

export const UserNotFoundErrorSchema = z.object({
error: z.literal("NotFound"),
message: z.literal("Failed to find user"),
});
export const ScanEventSchema = z
.object({
success: z.literal(true),
points: z.number().openapi({
description: "Points added from checking into the event",
example: 5,
}),
})
.openapi("ScanEvent");

export const UserNotFoundError: z.infer<typeof UserNotFoundErrorSchema> = {
export const [UserNotFoundError, UserNotFoundErrorSchema] = CreateErrorAndSchema({
error: "NotFound",
message: "Failed to find user",
};

export const EventNotFoundErrorSchema = z.object({
error: z.literal("NotFound"),
message: z.literal("Could not find event"),
});

export const EventNotFoundError: z.infer<typeof EventNotFoundErrorSchema> = {
export const [EventNotFoundError, EventNotFoundErrorSchema] = CreateErrorAndSchema({
error: "NotFound",
message: "Could not find event",
};
});

export type EventNotFoundError = typeof EventNotFoundError;

export const AlreadyCheckedInErrorSchema = z.object({
error: z.literal("AlreadyCheckedIn"),
message: z.literal("You're already checked in to this event"),
});

export const AlreadyCheckedInError: z.infer<typeof AlreadyCheckedInErrorSchema> = {
export const [AlreadyCheckedInError, AlreadyCheckedInErrorSchema] = CreateErrorAndSchema({
error: "AlreadyCheckedIn",
message: "You're already checked in to this event",
};
});

export type AlreadyCheckedInError = typeof AlreadyCheckedInError;

export const ScanEventSchema = z
.object({
success: z.literal(true),
points: z.number().openapi({
description: "Points added from checking into the event",
example: 5,
}),
})
.openapi("ScanEvent");

0 comments on commit 4b34cbc

Please sign in to comment.