diff --git a/controllers/challenges.ts b/controllers/challenges.ts index 5dab094..e1fea40 100644 --- a/controllers/challenges.ts +++ b/controllers/challenges.ts @@ -1,5 +1,6 @@ import DB from "@/database/config"; import { challenges } from "@/database/schema/challenges"; +import { clubs } from "@/database/schema/clubs"; import { eq } from "drizzle-orm"; export default abstract class ChallengesController { @@ -84,5 +85,17 @@ export default abstract class ChallengesController { await DB.instance.delete(challenges).where(eq(challenges.id, id)); } - public static async getDailyChallenges() {} + public static async getDailyChallenges() { + const dailyChallenges = await DB.instance + .select({ + id: challenges.id, + name: challenges.name, + score: challenges.score, + }) + .from(challenges) + .innerJoin(clubs, eq(challenges.clubId, clubs.id)) + .where(eq(clubs.dailyDate, new Date())); + + return dailyChallenges + } } diff --git a/routes/daily/challenges.ts b/routes/daily/challenges.ts index 7f563d7..841fc36 100644 --- a/routes/daily/challenges.ts +++ b/routes/daily/challenges.ts @@ -1,8 +1,24 @@ +import ChallengesController from "@/controllers/challenges"; +import Status from "@/models/status"; +import { NextFunction, Request, Response } from "express"; + /** * Handles the /daily/challenges route. * Retrieves the daily challenges information. * Sends a response with a status of 200 and the challenges information. */ -// export default async function Route_Daily_Challenges_Get(req: Request, res: Response, next: NextFunction) { -// const challenges = null -// } +export default async function Route_DailyChallenges_Get(req: Request, res: Response, next: NextFunction) { + const challenges = await ChallengesController.getDailyChallenges(); + + if (!challenges) { + return Status.send(req, next, { + status: 404, + error: "errors.notFound" + }) + } + + return Status.send(req, next, { + status: 200, + data: challenges + }); +} diff --git a/routes/daily/router.ts b/routes/daily/router.ts index 7894c32..788669c 100644 --- a/routes/daily/router.ts +++ b/routes/daily/router.ts @@ -1,9 +1,10 @@ import { Router } from "express"; +import Route_DailyChallenges_Get from "./challenges"; import Route_Daily_Read from "./read"; const dailyRouter = Router(); dailyRouter.get("/", Route_Daily_Read); -// dailyRouter.get("/challenges", Route_Daily_Challenges_Get); +dailyRouter.get("/challenges", Route_DailyChallenges_Get); export default dailyRouter; diff --git a/tests/e2e/clubs.test.ts b/tests/e2e/clubs.test.ts index fbf03df..8bec4f9 100644 --- a/tests/e2e/clubs.test.ts +++ b/tests/e2e/clubs.test.ts @@ -1,7 +1,7 @@ import createApp from "@/app"; -import { del, get, post, put } from "../utils"; import globals from "@/env/env"; import { toDateISOString, toDateString } from "@/utils/date"; +import { del, get, post, put } from "../utils"; const app = createApp("e2e-users"); @@ -74,7 +74,7 @@ describe("Test clubs", () => { name: "daily test", avatarUrl: "https://placehold.co/400", description: "description", - dailyDate: toDateString() // Today + dailyDate: toDateString(new Date("2024-10-31")) }, { "X-ADMIN-KEY": globals.env.ADMIN_TOKEN @@ -93,7 +93,7 @@ describe("Test clubs", () => { avatarUrl: "https://placehold.co/400", name: "daily test", description: "description", - dailyDate: toDateISOString(), + dailyDate: toDateISOString(new Date("2024-10-31")), createdAt: expect.any(String), updatedAt: expect.any(String) } @@ -129,7 +129,7 @@ describe("Test clubs", () => { avatarUrl: "https://placehold.co/400", name: "daily test", description: "description", - dailyDate: toDateISOString() + dailyDate: toDateISOString(new Date("2024-10-31")) } ]) } @@ -202,24 +202,4 @@ describe("Test clubs", () => { ] }); }); - - test("should get daily club", async () => { - const res = await get(app, "/daily"); - - expect(res.body).toStrictEqual({ - masterStatus: 200, - sentAt: expect.any(Number), - response: [ - { - status: 200, - success: true, - data: { - avatarUrl: "https://placehold.co/400", - name: "challenged", - description: "description" - } - } - ] - }); - }); }); diff --git a/tests/e2e/daily.test.ts b/tests/e2e/daily.test.ts new file mode 100644 index 0000000..b1ff2dc --- /dev/null +++ b/tests/e2e/daily.test.ts @@ -0,0 +1,107 @@ +import createApp from "@/app"; +import globals from "@/env/env"; +import { toDateString } from "@/utils/date"; +import { get, post } from "../utils"; + +const app = createApp("e2e-daily"); + +const testGlobals = { + clubId: 0, +}; + +describe("Daily challenges", () => { + + test("should create a daily club and challenge", async () => { + const resClub = await post( + app, + "/admin/clubs", + { + name: "daily club", + avatarUrl: "https://placehold.co/400", + description: "description", + dailyDate: toDateString() // Today + }, + { + "X-ADMIN-KEY": globals.env.ADMIN_TOKEN + } + ); + + testGlobals.clubId = resClub.body.response[0].data.id ?? "invalid"; + + const res = await post( + app, + "/admin/challenges", + { + clubId: testGlobals.clubId, + score: 100, + name: "daily challenge" + }, + { + "X-ADMIN-KEY": globals.env.ADMIN_TOKEN + } + ); + + expect(res.body).toStrictEqual({ + masterStatus: 201, + sentAt: expect.any(Number), + response: [ + { + status: 201, + success: true, + data: { + id: expect.any(Number), + score: 100, + name: "daily challenge", + clubId: testGlobals.clubId + } + } + ] + }); + + }); + + + test("should get daily club", async () => { + const res = await get(app, "/daily"); + + expect(res.body).toStrictEqual({ + masterStatus: 200, + sentAt: expect.any(Number), + response: [ + { + status: 200, + success: true, + data: { + avatarUrl: "https://placehold.co/400", + name: "daily club", + description: "description" + } + } + ] + }); + + }); + + test("should get daily challenges", async () => { + const res = await get(app, "/daily/challenges"); + + expect(res.body).toStrictEqual({ + masterStatus: 200, + sentAt: expect.any(Number), + response: [ + { + status: 200, + success: true, + data: expect.arrayContaining([ + { + id: expect.any(Number), + name: expect.any(String), + score: expect.any(Number) + } + ]) + } + ] + }); + }); + +}); \ No newline at end of file