Skip to content

Commit

Permalink
feat: add activities tasks routes (#145)
Browse files Browse the repository at this point in the history
* feat: add activity table and employee role

* feat: add route to add employee in activity team

* feat: add activity team route

* feat: get one activity team

* Merge branch 'main' into admin/feat/tournaments

* feat: modify activities database schema

* feat: modify activities schema

* fix: activities types

* fix: pnpm-lock

* feat: enhance activity routes

* fix: activities test

* fix: update migrations

* fix: lint project

* feat: add activity occurence route and tasks

* feat: add tasks route for insertion and get all

* feat: enhance task creation and add get one task route

* fix: update pnpm

* feat: add tests for activities and teams

* fix: lint for pull request

* fix: spelling error

* feat: add delete task

* feat: add tasks patch route

* feat: add tasks tests
  • Loading branch information
Metololo authored May 28, 2024
1 parent bf5a7e0 commit 6e46bf3
Show file tree
Hide file tree
Showing 3 changed files with 410 additions and 3 deletions.
38 changes: 37 additions & 1 deletion apps/api/src/handlers/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenAPIHono } from '@hono/zod-openapi';
import { supabase } from '../libs/supabase.js';
import { zodErrorHook } from '../libs/zodError.js';
import { createTask, getAllTasks, getOneTask } from '../routes/tasks.js';
import { createTask, deleteTask, getAllTasks, getOneTask, updateTask } from '../routes/tasks.js';
import { checkRole } from '../utils/context.js';
import { getPagination } from '../utils/pagnination.js';
import { Role, type Variables } from '../validators/general.js';
Expand Down Expand Up @@ -113,3 +113,39 @@ tasks.openapi(createTask, async (c) => {

return c.json(data, 201);
});

tasks.openapi(deleteTask, async (c) => {
const { id } = c.req.valid('param');
const user = c.get('user');
const roles = user.roles;
await checkRole(roles, false);

const { error } = await supabase.from('ACTIVITIES_TASKS').delete().eq('id', id);

if (error) {
return c.json({ error: 'Task not found' }, 404);
}

return c.json({ message: `Task with id ${id} deleted` }, 200);
});

tasks.openapi(updateTask, async (c) => {
const { id } = c.req.valid('param');
const { title, description, status, priority, id_employee } = c.req.valid('json');
const user = c.get('user');
const roles = user.roles;
await checkRole(roles, false);

const { data, error } = await supabase
.from('ACTIVITIES_TASKS')
.update({ title, description, status, priority, id_employee })
.eq('id', id)
.select()
.single();

if (error || !data) {
return c.json({ error: 'Failed to update task' }, 400);
}

return c.json(data, 200);
});
64 changes: 62 additions & 2 deletions apps/api/src/routes/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { createRoute, z } from '@hono/zod-openapi';
import authMiddleware from '../middlewares/auth.js';
import { queryAllSchema } from '../utils/pagnination.js';
import { badRequestSchema, notFoundSchema, serverErrorSchema } from '../validators/general.js';
import { badRequestSchema, idParamValidator, notFoundSchema, serverErrorSchema } from '../validators/general.js';
import { taskSchema } from '../validators/tasks.js';

export const getAllTasks = createRoute({
method: 'get',
path: '/activities_exceptions/{id}/tasks',
summary: 'Get all activity occurence tasks',
summary: 'Get all tasks of an activity occurence',
description: 'Get all activity occurence tasks',
request: {
params: z.object({ id: z.coerce.number().min(1) }),
Expand Down Expand Up @@ -86,3 +86,63 @@ export const createTask = createRoute({
},
tags: ['task'],
});

export const deleteTask = createRoute({
method: 'delete',
path: '/tasks/{id}',
summary: 'Delete a task',
description: 'Delete a task',
security: [{ Bearer: [] }],
middleware: authMiddleware,
request: {
params: z.object({ id: z.coerce.number().min(1) }),
},
responses: {
200: {
description: 'Successful response',
content: {
'application/json': {
schema: z.object({
message: z.string(),
}),
},
},
},
500: serverErrorSchema,
404: notFoundSchema,
},
tags: ['task'],
});

export const updateTask = createRoute({
method: 'patch',
path: '/tasks/{id}',
summary: 'Update a task',
description: 'Update a task',
security: [{ Bearer: [] }],
middleware: authMiddleware,
request: {
params: idParamValidator,
body: {
content: {
'application/json': {
schema: taskSchema.omit({ id: true, created_at: true, id_activity_exception: true }).partial(),
},
},
},
},
responses: {
200: {
description: 'Successful response',
content: {
'application/json': {
schema: taskSchema,
},
},
},
500: serverErrorSchema,
400: badRequestSchema,
404: notFoundSchema,
},
tags: ['task'],
});
Loading

0 comments on commit 6e46bf3

Please sign in to comment.