Skip to content

Commit

Permalink
feature(cca): Full sprint (#7)
Browse files Browse the repository at this point in the history
* Fix logger.

* Change plugin folder.

* Disallow same folder relative.

* Disable sso.

* Move cca models to separate folder.

* Change cca mode.

* Fix schema issues.

* Remove unused cca upload interfacd.

* Add cache handler to room list.

* Use signup in info.

* Add subcommittee to signup.

* Fix minor validation issues.
  • Loading branch information
juancarlovieri authored Aug 12, 2024
1 parent 119a3b2 commit eb6931c
Show file tree
Hide file tree
Showing 45 changed files with 238 additions and 150 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = {
},
],

"no-relative-import-paths/no-relative-import-paths": ["error", { allowSameFolder: true, prefix: "@" }],
"no-relative-import-paths/no-relative-import-paths": ["error", { prefix: "@" }],
"import/no-dynamic-require": 0,
"no-underscore-dangle": ["error", { allow: ["_id"] }],

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"files": ["@types/environment.d.ts", "@types/fastify/index.d.ts"],
"include": ["v2/**/*", "App.ts", "@types/*"],
"ts-node": { "files": true, "require": ["tsconfig-paths/register"] },
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
Expand Down
2 changes: 1 addition & 1 deletion v2/controllers/bid/create.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Bid } from "@/v2/models/bid";
import type { iJersey } from "@/v2/models/jersey";
import { Jersey } from "@/v2/models/jersey";
import { auth } from "@/v2/utils/auth";
import { auth } from "@/v2/plugins/auth";
import { isEligible } from "@/v2/utils/jersey";
import { logEvent, reportError } from "@/v2/utils/logger";
import { sendError, sendStatus } from "@/v2/utils/req_handler";
Expand Down
2 changes: 1 addition & 1 deletion v2/controllers/bid/info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bid } from "@/v2/models/bid";
import { BiddingInfo } from "@/v2/models/biddingInfo";
import { auth } from "@/v2/utils/auth";
import { auth } from "@/v2/plugins/auth";
import { logAndThrow, reportError } from "@/v2/utils/logger";
import { resBuilder, sendError, success } from "@/v2/utils/req_handler";
import type { FastifyReply, FastifyRequest, RouteOptions } from "fastify";
Expand Down
17 changes: 9 additions & 8 deletions v2/controllers/cca/info.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CcaInfo } from "@/v2/models/ccaInfo";
import { CcaSignup } from "@/v2/models/ccaSignup";
import { CcaInfo } from "@/v2/models/cca/ccaInfo";
import { CcaSignup } from "@/v2/models/cca/ccaSignup";
import { Server } from "@/v2/models/server";
import { auth } from "@/v2/utils/auth";
import { auth } from "@/v2/plugins/auth";
import { logAndThrow, reportError } from "@/v2/utils/logger";
import { resBuilder, sendError, success } from "@/v2/utils/req_handler";
import type { FastifyReply, FastifyRequest, RouteOptions } from "fastify";
Expand All @@ -15,13 +15,14 @@ const schema = {
info: {
$ref: `ccaInfo`,
},
ccas: {
signups: {
type: `array`,
items: {
$ref: `cca`,
$ref: `ccaSignup`,
},
},
isOpen: { type: `boolean` },
openTime: { type: `number` },
},
}),
},
Expand All @@ -34,18 +35,18 @@ async function handler(req: FastifyRequest, res: FastifyReply) {

const p = await Promise.allSettled([
CcaInfo.findOne({ user: user._id }).session(session.session),
CcaSignup.find({ user: user._id }).populate(`cca`).session(session.session),
CcaSignup.find({ user: user._id }).populate(`cca`).populate(`subcommittees`).session(session.session),
]);
const info = logAndThrow([p[0]], `Cca info retrieval error`)[0] || {
name: null,
telegram: null,
email: null,
};
const ccas = logAndThrow([p[1]], `CcaSignups parse error`)[0].map((c) => c.cca);
const signups = logAndThrow([p[1]], `CcaSignups parse error`)[0];

const isOpen = (await Server.findOne({ key: `ccaOpen` }).session(session.session))?.value;

return await success(res, { info, ccas, isOpen });
return await success(res, { info, signups, isOpen });
} catch (error) {
reportError(error, `Bid Info handler error`);
return sendError(res);
Expand Down
6 changes: 3 additions & 3 deletions v2/controllers/cca/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cca } from "@/v2/models/cca";
import { Cca } from "@/v2/models/cca/cca";
import { reportError } from "@/v2/utils/logger";
import { resBuilder, sendError, success } from "@/v2/utils/req_handler";
import type { FastifyReply, FastifyRequest, RouteOptions } from "fastify";
Expand All @@ -18,8 +18,8 @@ const schema = {
async function handler(req: FastifyRequest, res: FastifyReply) {
const session = req.session.get(`session`)!;
try {
const cca = await Cca.find().session(session.session);
return await success(res, cca);
const ccas = await Cca.find().populate("subcommittees").session(session.session);
return await success(res, ccas);
} catch (error) {
reportError(error, `Cca list handler error`);
return sendError(res);
Expand Down
68 changes: 49 additions & 19 deletions v2/controllers/cca/signup.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { Cca } from "@/v2/models/cca";
import type { iCca } from "@/v2/models/cca";
import { CcaInfo } from "@/v2/models/ccaInfo";
import type { iCcaInfo } from "@/v2/models/ccaInfo";
import { CcaSignup } from "@/v2/models/ccaSignup";
import { Cca } from "@/v2/models/cca/cca";
import type { iCca } from "@/v2/models/cca/cca";
import { CcaInfo } from "@/v2/models/cca/ccaInfo";
import type { iCcaInfo } from "@/v2/models/cca/ccaInfo";
import type { iCcaSignup } from "@/v2/models/cca/ccaSignup";
import { CcaSignup } from "@/v2/models/cca/ccaSignup";
import { CcaSubcommittee } from "@/v2/models/cca/ccaSubcommittee";
import { Server } from "@/v2/models/server";
import { auth } from "@/v2/utils/auth";
import { logEvent, reportError } from "@/v2/utils/logger";
import { auth } from "@/v2/plugins/auth";
import { logAndThrow, logEvent, reportError } from "@/v2/utils/logger";
import { sendError, sendStatus } from "@/v2/utils/req_handler";
import type { FastifyReply, FastifyRequest, RouteOptions } from "fastify";
import type { Server as HttpServer, IncomingMessage, ServerResponse } from "http";
import type { FromSchema } from "json-schema-to-ts";
import { ObjectId } from "mongodb";

const schema = {
body: {
type: `object`,
required: [`info`, `ccas`],
required: [`info`, `signups`],
properties: {
info: {
$ref: `ccaInfo`,
},
ccas: {
signups: {
type: `array`,
items: {
$ref: `cca`,
$ref: `ccaSignup`,
},
},
},
Expand All @@ -33,7 +36,7 @@ const schema = {
type iSchema = FromSchema<typeof schema.body>;
type iBody = Omit<iSchema, keyof { info: iCcaInfo; ccas: iCca[] }> & {
info: iCcaInfo;
ccas: iCca[];
signups: iCcaSignup[];
};

async function handler(req: FastifyRequest<{ Body: iBody }>, res: FastifyReply) {
Expand All @@ -45,30 +48,57 @@ async function handler(req: FastifyRequest<{ Body: iBody }>, res: FastifyReply)
return await sendStatus(res, 403, `CCA registration not open.`);
}

if (
req.body.signups.some(
(s) => !ObjectId.isValid(s.cca._id) || s.subcommittees.some((sub) => !ObjectId.isValid(sub._id)),
)
) {
return await sendStatus(res, 401, `Invalid id(s).`);
}

const user = req.session.get(`user`)!;
const ccas = await Cca.find({
_id: { $in: req.body.ccas.map((s) => s._id) },
_id: { $in: req.body.signups.map((s) => s.cca._id) },
}).session(session.session);
const info = {
user: user._id,
...req.body.info,
};

if (ccas.length !== req.body.ccas.length) {
if (ccas.length !== req.body.signups.length) {
return await sendStatus(res, 400, `Invalid CCA id(s).`);
}

const newCcas = ccas.map((c) => ({ user: user._id, cca: c._id }));
const validSubcommittees = logAndThrow(
await Promise.allSettled(
req.body.signups.map(async (signup) => {
const subcommittees = await CcaSubcommittee.find({
_id: { $in: signup.subcommittees.map((s) => s._id) },
cca: signup.cca._id,
}).session(session.session);

await CcaSignup.deleteMany({ user: user._id }).session(session.session);
await CcaInfo.deleteOne({ user: user._id }).session(session.session);
if (subcommittees.length !== signup.subcommittees.length) return false;
return true;
}),
),
`CCA signup subcommittee parse`,
);

await CcaSignup.create(newCcas, { session: session.session });
await CcaInfo.create([info], { session: session.session });
if (validSubcommittees.some((s) => !s)) {
return await sendStatus(res, 400, `Invalid subcommittee(s).`);
}

await logEvent(`USER SIGNUP CCA`, session, JSON.stringify({ ccas: newCcas, info }), user._id);
const newSignups = req.body.signups.map((c) => ({ user: user._id, ...c }));

try {
await CcaSignup.deleteMany({ user: user._id }).session(session.session);
await CcaInfo.deleteOne({ user: user._id }).session(session.session);

await CcaSignup.create(newSignups, { session: session.session });
await CcaInfo.create([info], { session: session.session });

await logEvent(`USER SIGNUP CCA`, session, JSON.stringify({ signups: newSignups, info }), user._id);

await session.commit();
} catch (error) {
reportError(error, `CCA signup transaction commit error.`);
Expand Down
2 changes: 1 addition & 1 deletion v2/controllers/ihg/admin/placements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Hall } from "@/v2/models/hall";
import type { iIhgPlacement } from "@/v2/models/ihgPlacement";
import { IhgPlacement } from "@/v2/models/ihgPlacement";
import { IhgSport } from "@/v2/models/ihgSport";
import { admin } from "@/v2/utils/auth";
import { admin } from "@/v2/plugins/auth";
import { logAndThrow, logEvent, reportError } from "@/v2/utils/logger";
import { sendError, sendStatus } from "@/v2/utils/req_handler";
import type { FastifyReply, FastifyRequest, RouteOptions } from "fastify";
Expand Down
2 changes: 1 addition & 1 deletion v2/controllers/jersey/eligible.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { auth } from "@/v2/utils/auth";
import { auth } from "@/v2/plugins/auth";
import { getEligible } from "@/v2/utils/jersey";
import { reportError } from "@/v2/utils/logger";
import { resBuilder, sendError, success } from "@/v2/utils/req_handler";
Expand Down
2 changes: 1 addition & 1 deletion v2/controllers/room/bid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RoomBidInfo } from "@/v2/models/roomBidInfo";
import { RoomBlock } from "@/v2/models/roomBlock";
import { Server } from "@/v2/models/server";
import type { iUser } from "@/v2/models/user";
import { auth } from "@/v2/utils/auth";
import { auth } from "@/v2/plugins/auth";
import { logAndThrow, logEvent, reportError } from "@/v2/utils/logger";
import { mail } from "@/v2/utils/mailer";
import type { MongoSession } from "@/v2/utils/mongoSession";
Expand Down
2 changes: 1 addition & 1 deletion v2/controllers/room/info.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RoomBid } from "@/v2/models/roomBid";
import { RoomBidInfo, type iRoomBidInfo } from "@/v2/models/roomBidInfo";
import { Server, type iServer } from "@/v2/models/server";
import { auth } from "@/v2/utils/auth";
import { auth } from "@/v2/plugins/auth";
import { logAndThrow, reportError } from "@/v2/utils/logger";
import { resBuilder, sendError, success } from "@/v2/utils/req_handler";
import { isEligible } from "@/v2/utils/room";
Expand Down
3 changes: 3 additions & 0 deletions v2/controllers/room/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Room } from "@/v2/models/room";
import { RoomBid } from "@/v2/models/roomBid";
import { RoomBlock } from "@/v2/models/roomBlock";
import { checkCache, setCache } from "@/v2/utils/cache_handler";
import { logAndThrow, reportError } from "@/v2/utils/logger";
import { resBuilder, sendError, success } from "@/v2/utils/req_handler";
import type { FastifyReply, FastifyRequest, RouteOptions } from "fastify";
Expand Down Expand Up @@ -70,7 +71,9 @@ const list: RouteOptions<Server, IncomingMessage, ServerResponse, Record<string,
method: `GET`,
url: `/list`,
schema,
preHandler: checkCache,
handler,
onSend: setCache,
};

export { list };
2 changes: 1 addition & 1 deletion v2/controllers/sso/callback.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { User } from "@/v2/models/user";
import * as auth from "@/v2/utils/auth";
import * as auth from "@/v2/plugins/auth";
import { logEvent, reportError } from "@/v2/utils/logger";
import { sendError, sendStatus } from "@/v2/utils/req_handler";
import { oauthController } from "@/v2/utils/sso";
Expand Down
2 changes: 1 addition & 1 deletion v2/controllers/team/info.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Member } from "@/v2/models/member";
import { auth } from "@/v2/utils/auth";
import { auth } from "@/v2/plugins/auth";
import { reportError } from "@/v2/utils/logger";
import { resBuilder, sendError, success } from "@/v2/utils/req_handler";
import type { FastifyReply, FastifyRequest, RouteOptions } from "fastify";
Expand Down
2 changes: 1 addition & 1 deletion v2/controllers/user/info.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { auth } from "@/v2/utils/auth";
import { auth } from "@/v2/plugins/auth";
import { reportError } from "@/v2/utils/logger";
import { resBuilder, sendError, success } from "@/v2/utils/req_handler";
import type { FastifyReply, FastifyRequest, RouteOptions } from "fastify";
Expand Down
2 changes: 1 addition & 1 deletion v2/controllers/user/login.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Server } from "@/v2/models/server";
import { User } from "@/v2/models/user";
import * as auth from "@/v2/utils/auth";
import * as auth from "@/v2/plugins/auth";
import { logEvent, reportError } from "@/v2/utils/logger";
import { resBuilder, sendError, sendStatus, success } from "@/v2/utils/req_handler";
import * as bcrypt from "bcryptjs";
Expand Down
2 changes: 1 addition & 1 deletion v2/controllers/user/password-reset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { User } from "@/v2/models/user";
import { auth } from "@/v2/utils/auth";
import { auth } from "@/v2/plugins/auth";
import { reportError } from "@/v2/utils/logger";
import { sendError, sendStatus } from "@/v2/utils/req_handler";
import bcrypt from "bcryptjs";
Expand Down
4 changes: 2 additions & 2 deletions v2/models/bid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "./jersey";
import "./user";
import "@/v2/models/jersey";
import "@/v2/models/user";
import type { Document, Types } from "mongoose";
import { Schema, model } from "mongoose";

Expand Down
2 changes: 1 addition & 1 deletion v2/models/biddingInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "./jersey";
import "@/v2/models/jersey";
import type { Document, Types } from "mongoose";
import { Schema, model } from "mongoose";

Expand Down
36 changes: 19 additions & 17 deletions v2/models/cca.ts → v2/models/cca/cca.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ interface iCca extends Document {
heads: string[];
contacts: string[];
description: string;
committees: string[];
}

const rCca = {
Expand All @@ -28,27 +27,30 @@ const rCca = {
items: { type: `string` },
},
description: { type: `string` },
committees: {
type: `array`,
items: { type: `string` },
},
subcommittees: { type: `array`, items: { $ref: `ccaSubcommittee` } },
},
additionalProperties: false,
};

const ccaSchema = new Schema<iCca>({
name: { type: String, required: true, index: 1 },
category: { type: String },
heads: {
type: [String],
},
contacts: {
type: [String],
},
description: { type: String },
committees: {
type: [String],
const ccaSchema = new Schema<iCca>(
{
name: { type: String, required: true, index: 1 },
category: { type: String },
heads: {
type: [String],
},
contacts: {
type: [String],
},
description: { type: String },
},
{ toObject: { virtuals: true }, toJSON: { virtuals: true } },
);

ccaSchema.virtual(`subcommittees`, {
ref: `CcaSubcommittee`,
localField: `_id`,
foreignField: `cca`,
});

const Cca = model<iCca>(`Cca`, ccaSchema);
Expand Down
File renamed without changes.
Loading

0 comments on commit eb6931c

Please sign in to comment.