Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/user dashboard #21

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/controllers/competition.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import {
getAnnouncementsByCompetitionId,
getCompetition,
getCompetitionParticipant,
getCompetitionTimelines,
getCompetitionTimelinesByCompetitionId,
postAnnouncement,
} from '~/repositories/competition.repository';
import {
getAdminCompAnnouncementRoute,
getCompetitionParticipantRoute,
getCompetitionTimeLineByCompetitionIdRoute,
getCompetitionTimelineRoute,
postAdminCompAnnouncementRoute,
} from '~/routes/competition.route';
import { createAuthRouter } from '~/utils/router-factory';
Expand Down Expand Up @@ -79,3 +83,22 @@ competitionProtectedRouter.openapi(
return c.json(announcement, 200);
},
);

competitionProtectedRouter.openapi(getCompetitionTimelineRoute, async (c) => {
const user = c.get('user');
const userId = user.id;
const timelines = await getCompetitionTimelines(db, userId);
return c.json(timelines, 200);
});

competitionProtectedRouter.openapi(
getCompetitionTimeLineByCompetitionIdRoute,
async (c) => {
const { competitionId } = c.req.valid('param');
const timelines = await getCompetitionTimelinesByCompetitionId(
db,
competitionId,
);
return c.json(timelines, 200);
},
);
10 changes: 10 additions & 0 deletions src/controllers/team.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,23 @@ teamProtectedRouter.openapi(postTeamDocumentRoute, async (c) => {
return c.json(updatedTeam, 200);
});

teamProtectedRouter.get(
getTeamCompetitionRoute.getRoutingPath(),
roleMiddleware('admin'),
);

teamProtectedRouter.openapi(getTeamCompetitionRoute, async (c) => {
const { competitionId } = c.req.valid('param');
const teams = await getTeamsByCompetitionId(db, competitionId);
if (!teams) return c.json({ error: "Competition doesn't exist!" }, 400);
return c.json(teams, 200);
});

teamProtectedRouter.get(
getTeamDetailRoute.getRoutingPath(),
roleMiddleware('admin'),
);

teamProtectedRouter.openapi(getTeamDetailRoute, async (c) => {
const { teamId } = c.req.valid('param');
const team = await getTeamById(db, teamId, { teamMember: true });
Expand Down
34 changes: 33 additions & 1 deletion src/repositories/competition.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { first } from '~/db/helper';
import type { PostCompAnnouncementBodySchema } from '~/types/competition.type';

import type { Database } from '../db/drizzle';
import { competition, competitionAnnouncement, team } from '../db/schema';
import {
competition,
competitionAnnouncement,
competitionTimeline,
team,
teamMember,
} from '../db/schema';

export const getCompetitionParticipantNumber = async (
db: Database,
Expand Down Expand Up @@ -97,3 +103,29 @@ export const postAnnouncement = async (
.returning()
.then(first);
};

export const getCompetitionTimelines = async (db: Database, userId: string) => {
// Get user's competitions
const userTeams = await db.query.team.findMany({
where: eq(teamMember.userId, userId),
with: {
competition: {
with: {
timeline: true,
},
},
},
});

return userTeams.flatMap((team) => team.competition.timeline);
};

export const getCompetitionTimelinesByCompetitionId = async (
db: Database,
competitionId: string,
) => {
const result = await db.query.competitionTimeline.findMany({
where: eq(competitionTimeline.competitionId, competitionId),
});
return result;
};
42 changes: 42 additions & 0 deletions src/routes/competition.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createRoute } from '@hono/zod-openapi';
import {
AnnouncementSchema,
CompetitionParticipantSchema,
CompetitionTimelineSchema,
GetCompetitionTimeQuerySchema,
PostCompAnnouncementBodySchema,
} from '~/types/competition.type';
Expand Down Expand Up @@ -83,3 +84,44 @@ export const postAdminCompAnnouncementRoute = createRoute({
500: createErrorResponse('GENERIC', 'Internal server error'),
},
});

export const getCompetitionTimelineRoute = createRoute({
operationId: 'getCompetitionTimeline',
tags: ['competition'],
method: 'get',
path: '/competition/timeline',
responses: {
200: {
content: {
'application/json': {
schema: CompetitionTimelineSchema,
},
},
description: 'Successfully fetched competition timelines',
},
400: createErrorResponse('UNION', 'Bad request error'),
500: createErrorResponse('GENERIC', 'Internal server error'),
},
});

export const getCompetitionTimeLineByCompetitionIdRoute = createRoute({
operationId: 'getCompetitionTimelineWithCompetitionId',
tags: ['competition'],
method: 'get',
path: '/competition/{competitionId}/timeline',
request: {
params: CompetitionIdParam,
},
responses: {
200: {
content: {
'application/json': {
schema: CompetitionTimelineSchema,
},
},
description: 'Successfully fetched competition timelines',
},
400: createErrorResponse('UNION', 'Bad request error'),
500: createErrorResponse('GENERIC', 'Internal server error'),
},
});
6 changes: 5 additions & 1 deletion src/types/competition.type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSelectSchema } from 'drizzle-zod';
import { z } from 'zod';
import { competitionAnnouncement } from '~/db/schema';
import { competitionAnnouncement, competitionTimeline } from '~/db/schema';

import { TeamSchema } from './team.type';

Expand Down Expand Up @@ -57,3 +57,7 @@ export const GetCompetitionTimeQuerySchema = z.object({
},
}),
});

export const CompetitionTimelineSchema = z.array(
createSelectSchema(competitionTimeline).openapi('CompetitionTimeline'),
);
Loading