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/program planner #68

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions backend/@types/express/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Bed,
Room,
File,
ProgramEvent,
} from '@prisma/client';

declare global {
Expand All @@ -28,6 +29,7 @@ declare global {
room?: Room & { beds: Bed[] };
bed?: Bed;
file?: File;
programEvent?: ProgramEvent;
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- CreateTable
CREATE TABLE `program_events` (
`id` CHAR(26) NOT NULL,
`camp_id` CHAR(36) NOT NULL,
`title` JSON NOT NULL,
`details` JSON NULL,
`location` JSON NULL,
`date` VARCHAR(191) NULL,
`duration` INTEGER NULL,
`time` VARCHAR(191) NULL,
`background_color` VARCHAR(191) NULL,
`side` VARCHAR(191) NULL,

UNIQUE INDEX `program_event_id_unique`(`id`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `program_events` ADD CONSTRAINT `program_events_camp_id_fkey` FOREIGN KEY (`camp_id`) REFERENCES `camps`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
20 changes: 20 additions & 0 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ model Camp {
rooms Room[]
templates TableTemplate[]
files File[]
ProgramEvent ProgramEvent[]

@@map("camps")
}
Expand Down Expand Up @@ -184,3 +185,22 @@ model File {

@@map("files")
}

model ProgramEvent {
id String @id @unique(map: "program_event_id_unique") @db.Char(26)
campId String @map("camp_id") @db.Char(36) // Prisma does not support morth
/// [StringOrTranslation]
title Json
/// [StringOrTranslation]
details Json?
/// [StringOrTranslation]
location Json?
date String?
duration Int?
time String?
color String? @map("color")
side String?
camp Camp? @relation(fields: [campId], references: [id], onDelete: Cascade)

@@map("program_events")
}
2 changes: 2 additions & 0 deletions backend/src/app/camp/camp.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import registrationRoutes from 'app/registration/registration.routes';
import tableTemplateRoutes from 'app/tableTemplate/table-template.routes';
import roomRoutes from 'app/room/room.routes';
import campFileRoutes from './camp-files.routes';
import programEventRoutes from 'app/programEvent/program-event.routes';
import { CampCreateData, CampQuery } from '@camp-registration/common/entities';

const router = express.Router();
Expand Down Expand Up @@ -51,6 +52,7 @@ router.use('/:campId/templates', tableTemplateRoutes);
router.use('/:campId/managers', managerRoutes);
router.use('/:campId/rooms', roomRoutes);
router.use('/:campId/files', campFileRoutes);
router.use('/:campId/program-events', programEventRoutes);

router.get(
'/',
Expand Down
73 changes: 73 additions & 0 deletions backend/src/app/programEvent/program-event.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { catchRequestAsync } from 'utils/catchAsync';
import httpStatus from 'http-status';
import { collection, resource } from 'app/resource';
import programEventService from './program-event.service';
import programEventResource from './program-event.resource';
import { routeModel } from 'utils/verifyModel';

const show = catchRequestAsync(async (req, res) => {
const event = routeModel(req.models.programEvent);

res.json(resource(programEventResource(event)));
});

const index = catchRequestAsync(async (req, res) => {
const { campId } = req.params;

const events = await programEventService.queryProgramEvent(campId);
const resources = events.map((value) => programEventResource(value));

res.json(collection(resources));
});

const store = catchRequestAsync(async (req, res) => {
const { campId } = req.params;
const data = req.body;

const event = await programEventService.createProgramEvent(campId, {
title: data.title,
details: data.details,
location: data.location,
date: data.date,
time: data.time,
duration: data.duration,
color: data.color,
side: data.side,
});

res.status(httpStatus.CREATED).json(resource(programEventResource(event)));
});

const update = catchRequestAsync(async (req, res) => {
const { programEventId: id } = req.params;
const data = req.body;

const event = await programEventService.updateProgramEventById(id, {
title: data.title,
details: data.details,
location: data.location,
date: data.date,
time: data.time,
duration: data.duration,
color: data.color,
side: data.side,
});

res.json(resource(programEventResource(event)));
});

const destroy = catchRequestAsync(async (req, res) => {
const { programEventId: id } = req.params;

await programEventService.deleteProgramEventById(id);

res.status(httpStatus.NO_CONTENT).send();
});

export default {
index,
show,
store,
update,
destroy,
};
20 changes: 20 additions & 0 deletions backend/src/app/programEvent/program-event.resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ProgramEvent } from '@prisma/client';
import type { ProgramEvent as ProgramEventResource } from '@camp-registration/common/entities';

const programEventResource = (
programEvent: ProgramEvent,
): ProgramEventResource => {
return {
id: programEvent.id,
title: programEvent.title,
details: programEvent.details,
location: programEvent.location,
date: programEvent.date ?? null,
time: programEvent.time ?? null,
duration: programEvent.duration ?? null,
color: programEvent.color ?? 'white',
side: (programEvent.side as ProgramEventResource['side']) ?? null,
};
};

export default programEventResource;
57 changes: 57 additions & 0 deletions backend/src/app/programEvent/program-event.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import express from 'express';
import { auth, guard, validate } from 'middlewares';
import { campManager } from 'guards';
import { routeModel, verifyModelExists } from 'utils/verifyModel';
import { catchParamAsync } from 'utils/catchAsync';
import programPlannerService from './program-event.service';
import programEventController from './program-event.controller';
import programEventValidation from './program-event.validation';

const router = express.Router({ mergeParams: true });

router.param(
'programEventId',
catchParamAsync(async (req, res, id) => {
const camp = routeModel(req.models.camp);
const event = await programPlannerService.getProgramEventById(camp.id, id);
req.models.programEvent = verifyModelExists(event);
}),
);

router.get(
'/',
auth(),
guard(campManager),
validate(programEventValidation.index),
programEventController.index,
);
router.get(
'/:programEventId',
auth(),
guard(campManager),
validate(programEventValidation.show),
programEventController.show,
);
router.post(
'/',
auth(),
guard(campManager),
validate(programEventValidation.store),
programEventController.store,
);
router.put(
'/:programEventId',
auth(),
guard(campManager),
validate(programEventValidation.update),
programEventController.update,
);
router.delete(
'/:programEventId',
auth(),
guard(campManager),
validate(programEventValidation.destroy),
programEventController.destroy,
);

export default router;
50 changes: 50 additions & 0 deletions backend/src/app/programEvent/program-event.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { type Prisma } from '@prisma/client';
import prisma from 'client';
import { ulid } from 'utils/ulid';

const getProgramEventById = async (campId: string, id: string) => {
return prisma.programEvent.findFirst({
where: { id, campId },
});
};

const queryProgramEvent = async (campId: string) => {
return prisma.programEvent.findMany({
where: { campId },
});
};

const createProgramEvent = async (
campId: string,
data: Omit<Prisma.ProgramEventCreateInput, 'id' | 'camp'>,
) => {
return prisma.programEvent.create({
data: {
id: ulid(),
campId,
...data,
},
});
};

const updateProgramEventById = async (
id: string,
data: Omit<Prisma.ProgramEventUpdateInput, 'id'>,
) => {
return prisma.programEvent.update({
where: { id: id },
data,
});
};

const deleteProgramEventById = async (id: string) => {
await prisma.programEvent.delete({ where: { id: id } });
};

export default {
getProgramEventById,
queryProgramEvent,
createProgramEvent,
updateProgramEventById,
deleteProgramEventById,
};
72 changes: 72 additions & 0 deletions backend/src/app/programEvent/program-event.validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import Joi from 'joi';
import JoiDate from '@joi/date';
import { ProgramEvent } from '@camp-registration/common/entities';

const extendedJoi = Joi.extend(JoiDate);

const translatableSchema = Joi.alternatives()
.try(Joi.string(), Joi.object().pattern(Joi.string(), Joi.string()))
.allow('');
const timeSchema = Joi.string().regex(/^([01]\d|2[0-3]):([0-5]\d)$/);
const dateSchema = extendedJoi.date().format('YYYY-MM-DD').raw();

const show = {
params: Joi.object({
campId: Joi.string().required(),
programEventId: Joi.string().required(),
}),
};

const index = {
params: Joi.object({
campId: Joi.string().required(),
}),
};

const store = {
params: Joi.object({
campId: Joi.string().required(),
}),
body: Joi.object<ProgramEvent>({
title: translatableSchema.required(),
details: translatableSchema.optional().allow(null),
location: translatableSchema.optional().allow(null),
date: dateSchema.optional(),
time: timeSchema.optional().allow(null),
duration: Joi.number().min(0).optional().allow(null),
color: Joi.string().optional().allow(null),
side: Joi.string().optional().allow(null),
}),
};

const update = {
params: Joi.object({
campId: Joi.string().required(),
programEventId: Joi.string().required(),
}),
body: Joi.object({
title: translatableSchema.optional(),
details: translatableSchema.optional().allow(null),
location: translatableSchema.optional().allow(null),
date: dateSchema.optional(),
time: timeSchema.optional().allow(null),
duration: Joi.number().min(0).optional().allow(null),
color: Joi.string().optional().allow(null),
side: Joi.string().optional().allow(null),
}),
};

const destroy = {
params: Joi.object({
campId: Joi.string().required(),
programEventId: Joi.string().required(),
}),
};

export default {
show,
index,
store,
update,
destroy,
};
2 changes: 0 additions & 2 deletions backend/tests/integration/camp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ describe('/api/v1/camps', () => {

expect(status).toBe(200);

console.log(body.data);

expect(body).toHaveProperty('data');
expect(body.data.length).toBe(2);
});
Expand Down
6 changes: 3 additions & 3 deletions common/src/entities/Expense.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export interface Expense {
id: number;
name: string;
category?: string;
category: string | null;
price: number;
paid: boolean;
paidBy: string;
date?: string;
recipient?: string;
date: string | null;
recipient: string | null;
}
Loading