Skip to content

Commit

Permalink
Merge pull request #6 from La-404-Devinci/feat/daily
Browse files Browse the repository at this point in the history
  • Loading branch information
Drixares authored Nov 2, 2024
2 parents e7b95fb + 11c0f27 commit 3c8dccb
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 31 deletions.
15 changes: 14 additions & 1 deletion controllers/challenges.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
}
}
22 changes: 19 additions & 3 deletions routes/daily/challenges.ts
Original file line number Diff line number Diff line change
@@ -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
});
}
3 changes: 2 additions & 1 deletion routes/daily/router.ts
Original file line number Diff line number Diff line change
@@ -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;
32 changes: 6 additions & 26 deletions tests/e2e/clubs.test.ts
Original file line number Diff line number Diff line change
@@ -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");

Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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"))
}
])
}
Expand All @@ -148,7 +148,7 @@ describe("Test clubs", () => {
name: "challenged",
avatarUrl: "https://placehold.co/400",
description: "description",
dailyDate: toDateString()
dailyDate: toDateString(new Date("2024-10-31"))
},
{
"X-ADMIN-KEY": globals.env.ADMIN_TOKEN
Expand All @@ -169,7 +169,7 @@ describe("Test clubs", () => {
avatarUrl: "https://placehold.co/400",
name: "challenged",
description: "description",
dailyDate: toDateISOString(),
dailyDate: toDateISOString(new Date("2024-10-31")),
createdAt: expect.any(String),
updatedAt: expect.any(String)
}
Expand Down Expand Up @@ -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"
}
}
]
});
});
});
102 changes: 102 additions & 0 deletions tests/e2e/daily.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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)
}
])
}
]
});
});
});

0 comments on commit 3c8dccb

Please sign in to comment.