All files / server/src/features/direct-signup directSignupSchema.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 4/4
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            60x     1754x 1754x       60x               60x         1754x           1754x           60x                       60x   60x  
// Registers the global mongoose plugins, which are applied when a model is
// compiled below, so it has to stay above the other imports
import "server/db/mongoosePlugins";
import mongoose from "mongoose";
import { z } from "zod";
 
const UserSignupsSchema = z.object({
  username: z.string(),
  priority: z.number(),
  signedToStartTime: z.date().transform((date) => date.toISOString()),
  signupTime: z.date().transform((date) => date.toISOString()),
  message: z.string(),
});
 
export const DirectSignupSchemaDb = z
  .object({
    programItemId: z.string(),
    userSignups: z.array(UserSignupsSchema),
    count: z.number(),
  })
  .strip();
 
const userSignupSchema = new mongoose.Schema({
  username: { type: String, required: true },
  priority: { type: Number, required: true },
  signedToStartTime: {
    type: Date,
    get: (value: Date) => new Date(value),
    required: true,
  },
  // Timestamp recording when the user signed up
  signupTime: {
    type: Date,
    get: (value: Date) => new Date(value),
    required: true,
  },
  message: { type: String, required: true },
});
 
const directSignupSchema = new mongoose.Schema(
  {
    programItemId: { type: String, required: true },
    userSignups: { type: [userSignupSchema], required: true },
    count: { type: Number, default: 0 },
  },
  { timestamps: true },
);
 
// One sign-up document per program item, which every query against this collection assumes.
// Ensuring a document exists is a read followed by a write, so without this two overlapping
// program item imports could each insert one
directSignupSchema.index({ programItemId: 1 }, { unique: true });
 
export const SignupModel = mongoose.model("direct-signup", directSignupSchema);