Skip to content

Commit

Permalink
refactor: tests workflows & clean search querys in api (#114)
Browse files Browse the repository at this point in the history
* fix: activities test workflow

* refactor: optimize tests files

* refactor: utils test

* fix: type cov

* refactor(api): search terms querys

* fix: missing all parameters activities

* test: add sports test workflow

* fix: tests

* fix: user tests

* fix(test): activities test

* fix: console
  • Loading branch information
Jayllyz authored May 7, 2024
1 parent 95ea43b commit 68b6960
Show file tree
Hide file tree
Showing 30 changed files with 490 additions and 378 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ jobs:

- name: 🛠️ Setup pnpm
uses: pnpm/action-setup@v3
with:
version: 9

- name: 🛠️ Set up Node.js
uses: actions/setup-node@v4
Expand Down
2 changes: 1 addition & 1 deletion apps/admin/app/ui/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bell, CircleUser, Home, LineChart, Menu, Package, Package2, Search, ShoppingCart, Users } from 'lucide-react';
import { CircleUser, Home, LineChart, Menu, Package, Package2, Search, ShoppingCart, Users } from 'lucide-react';
import Link from 'next/link';

import { Badge } from '@repo/ui/components/ui/badge';
Expand Down
1 change: 0 additions & 1 deletion apps/admin/app/ui/dashboard/activities/EditForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { format } from 'date-fns';
import { fr } from 'date-fns/locale';
import { Calendar as CalendarIcon } from 'lucide-react';
import { useRouter } from 'next/navigation';
import type React from 'react';
import type { Dispatch, SetStateAction } from 'react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
Expand Down
2 changes: 1 addition & 1 deletion apps/admin/app/ui/dashboard/votes/editVotes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface Props {
function EditVote({ votes, setVotes, id, setEditingVote }: Props) {
const { toast } = useToast();
const router = useRouter();
const editedVote = votes.find((vote) => vote.id === id);
const editedVote: Vote | undefined = votes.find((vote: Vote) => vote.id === id);

const formSchema = z.object({
title: z.string().min(2, { message: 'Le titre doit contenir au moins 2 caractères' }),
Expand Down
2 changes: 1 addition & 1 deletion apps/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"devDependencies": {
"@repo/biome-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@types/node": "^20.12.8",
"@types/node": "^20.12.10",
"@types/react": "^18.3.1",
"@types/react-dom": "^18.3.0",
"autoprefixer": "^10.4.19",
Expand Down
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"devDependencies": {
"@repo/biome-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@types/node": "^20.12.8",
"@types/node": "^20.12.10",
"@types/swagger-ui-dist": "^3.30.4",
"supabase": "^1.165.0",
"tsx": "^4.9.3",
Expand Down
24 changes: 15 additions & 9 deletions apps/api/src/handlers/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,28 @@ export const activities = new OpenAPIHono<{ Variables: Variables }>({
});

activities.openapi(getAllActivities, async (c) => {
const { search, skip, take } = c.req.valid('query');
const { from, to } = getPagination(skip, take - 1);
const { data, error } = await supabase
.from('ACTIVITIES')
.select('*', { count: 'exact' })
.range(from, to)
.order('id', { ascending: true })
.ilike('name', `%${search}%`);
const { all, search, skip, take } = c.req.valid('query');

const query = supabase.from('ACTIVITIES').select('*', { count: 'exact' }).order('id', { ascending: true });

if (search) {
query.ilike('name', `%${search}%`);
}

if (!all) {
const { from, to } = getPagination(skip, take - 1);
query.range(from, to);
}

const { data, error, count } = await query;

if (error) {
return c.json({ error: error.message }, 500);
}

const responseData = {
data: data || [],
count: data?.length || 0,
count: count || 0,
};

return c.json(responseData, 200);
Expand Down
44 changes: 35 additions & 9 deletions apps/api/src/handlers/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,26 @@ export const blog = new OpenAPIHono<{ Variables: Variables }>({
});

blog.openapi(getAllPosts, async (c) => {
const { skip, take } = c.req.valid('query');
const { from, to } = getPagination(skip, take - 1);
const { data, error } = await supabase.from('POSTS').select('*').range(from, to);
const { all, search, skip, take } = c.req.valid('query');

const query = supabase.from('POSTS').select('*', { count: 'exact' }).order('id', { ascending: true });

if (search) {
query.ilike('title', `%${search}%`);
}

if (!all) {
const { from, to } = getPagination(skip, take - 1);
query.range(from, to);
}

const { data, error, count } = await query;

if (error) {
return c.json({ error: error.message }, 500);
}

return c.json(data, 200);
return c.json({ data, count }, 200);
});

blog.openapi(getPost, async (c) => {
Expand Down Expand Up @@ -129,15 +140,30 @@ blog.openapi(commentOnPost, async (c) => {

blog.openapi(getComments, async (c) => {
const { id } = c.req.valid('param');
const { skip, take } = c.req.valid('query');
const { from, to } = getPagination(skip, take - 1);
const { data, error } = await supabase.from('COMMENTS').select('*').eq('id_post', id).range(from, to);
const { all, search, skip, take } = c.req.valid('query');

const query = supabase
.from('COMMENTS')
.select('*', { count: 'exact' })
.eq('id_post', id)
.order('id', { ascending: true });

if (search) {
query.ilike('content', `%${search}%`);
}

if (!all) {
const { from, to } = getPagination(skip, take - 1);
query.range(from, to);
}

const { data, error, count } = await query;

if (error) {
return c.json({ error: error.message }, 500);
return c.json({ error: "Failed to get comments, verify the post's id" }, 400);
}

return c.json(data, 200);
return c.json({ data, count }, 200);
});

blog.openapi(createResponse, async (c) => {
Expand Down
31 changes: 8 additions & 23 deletions apps/api/src/handlers/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,19 @@ export const location = new OpenAPIHono<{ Variables: Variables }>({

location.openapi(getAllAddresses, async (c) => {
const { all, search, skip, take } = c.req.valid('query');
const searchTerm = search || '';

if (all) {
const { data, error, count } = await supabase
.from('ADDRESSES')
.select('*', { count: 'exact' })
.order('id', { ascending: true })
.ilike('road', `%${searchTerm}%`);
const query = supabase.from('ADDRESSES').select('*', { count: 'exact' }).order('id', { ascending: true });

if (error) {
return c.json({ error: error.message }, 500);
}

const responseData = {
data: data || [],
count: count || 0,
};
if (search) {
query.ilike('road', `%${search}%`);
}

return c.json(responseData, 200);
if (!all) {
const { from, to } = getPagination(skip, take - 1);
query.range(from, to);
}

const { from, to } = getPagination(skip, take - 1);
const { data, error, count } = await supabase
.from('ADDRESSES')
.select('*', { count: 'exact' })
.range(from, to)
.order('id', { ascending: true })
.ilike('road', `%${searchTerm}%`);
const { data, error, count } = await query;

if (error) {
return c.json({ error: error.message }, 500);
Expand Down
37 changes: 11 additions & 26 deletions apps/api/src/handlers/sports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,19 @@ export const sports = new OpenAPIHono<{ Variables: Variables }>({

sports.openapi(getAllSports, async (c) => {
const { search, all, skip, take } = c.req.valid('query');
const searchTerm = search || '';

if (all) {
const { data, error, count } = await supabase
.from('SPORTS')
.select('*', { count: 'exact' })
.order('id', { ascending: true })
.ilike('name', `%${searchTerm}%`);
const query = supabase.from('SPORTS').select('*', { count: 'exact' }).order('id', { ascending: true });

if (error) {
return c.json({ error: error.message }, 500);
}

const responseData = {
data: data || [],
count: count || 0,
};
if (search) {
query.ilike('name', `%${search}%`);
}

return c.json(responseData, 200);
if (!all) {
const { from, to } = getPagination(skip, take - 1);
query.range(from, to);
}

const { from, to } = getPagination(skip, take - 1);
const { data, error, count } = await supabase
.from('SPORTS')
.select('*', { count: 'exact' })
.range(from, to)
.order('id', { ascending: true })
.ilike('name', `%${searchTerm}%`);
const { data, error, count } = await query;

if (error) {
return c.json({ error: error.message }, 500);
Expand Down Expand Up @@ -110,11 +95,11 @@ sports.openapi(deleteSport, async (c) => {
const roles = user.roles;
await checkRole(roles, false);

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

if (error || !data) {
if (error) {
return c.json({ error: 'Sport not found' }, 404);
}

return c.json(data, 200);
return c.json({ message: `Sport with id ${id} deleted` }, 200);
});
21 changes: 14 additions & 7 deletions apps/api/src/handlers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,24 @@ export const users = new OpenAPIHono<{ Variables: Variables }>({
users.openapi(getAllUsers, async (c) => {
const roles = c.get('user').roles;
await checkRole(roles, false, [Role.ADMIN]);
const { search, skip, take } = c.req.valid('query');
const searchTerm = search || '';
const { search, all, skip, take } = c.req.valid('query');

const { from, to } = getPagination(skip, take);
const { data, error, count } = await supabase
const query = supabase
.from('USERS')
.select('*, roles:ROLES (id, name)', { count: 'exact' })
.range(from, to)
.order('created_at', { ascending: true })
.filter('deleted_at', 'is', null)
.ilike('username', `%${searchTerm}%`);
.order('created_at', { ascending: true });

if (search) {
query.ilike('username', `%${search}%`);
}

if (!all) {
const { from, to } = getPagination(skip, take - 1);
query.range(from, to);
}

const { data, error, count } = await query;

if (error) {
return c.json({ error: error.message }, 500);
Expand Down
7 changes: 2 additions & 5 deletions apps/api/src/routes/activities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRoute, z } from '@hono/zod-openapi';
import authMiddleware from '../middlewares/auth.js';
import { paginationSchema } from '../utils/pagnination.js';
import { queryAllSchema } from '../utils/pagnination.js';
import { idParamValidator, notFoundSchema, serverErrorSchema } from '../validators/general.js';

export const activitySchema = z.object({
Expand All @@ -24,10 +24,7 @@ export const getAllActivities = createRoute({
summary: 'Get all activities',
description: 'Get all activities',
request: {
query: z.object({
search: z.string().optional(),
...paginationSchema.shape,
}),
query: queryAllSchema,
},
responses: {
200: {
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/routes/blog.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createRoute } from '@hono/zod-openapi';
import { number, z } from 'zod';
import authMiddleware from '../middlewares/auth.js';
import { paginationSchema } from '../utils/pagnination.js';
import { queryAllSchema } from '../utils/pagnination.js';
import {
commentSchema,
insertCommentSchema,
Expand All @@ -19,7 +19,7 @@ export const getAllPosts = createRoute({
summary: 'Get all posts',
description: 'Get all posts',
request: {
query: paginationSchema,
query: queryAllSchema,
},
responses: {
200: {
Expand Down Expand Up @@ -194,7 +194,7 @@ export const getComments = createRoute({
description: 'Get comments and responses on a post',
request: {
params: idParamValidator,
query: paginationSchema,
query: queryAllSchema,
},
responses: {
200: {
Expand Down
8 changes: 2 additions & 6 deletions apps/api/src/routes/locations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRoute, z } from '@hono/zod-openapi';
import authMiddleware from '../middlewares/auth.js';
import { paginationSchema } from '../utils/pagnination.js';
import { queryAllSchema } from '../utils/pagnination.js';
import { idParamValidator, notFoundSchema, serverErrorSchema } from '../validators/general.js';

export const addressSchema = z.object({
Expand Down Expand Up @@ -50,11 +50,7 @@ export const getAllAddresses = createRoute({
summary: 'Get all addresses',
description: 'Get all addresses',
request: {
query: z.object({
search: z.string().optional(),
all: z.coerce.boolean().optional(),
...paginationSchema.shape,
}),
query: queryAllSchema,
},
responses: {
200: {
Expand Down
8 changes: 2 additions & 6 deletions apps/api/src/routes/sports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRoute, z } from '@hono/zod-openapi';
import authMiddleware from '../middlewares/auth.js';
import { paginationSchema } from '../utils/pagnination.js';
import { queryAllSchema } from '../utils/pagnination.js';
import { idParamValidator, notFoundSchema, serverErrorSchema } from '../validators/general.js';

export const sportSchema = z.object({
Expand All @@ -18,11 +18,7 @@ export const getAllSports = createRoute({
summary: 'Get all sports',
description: 'Get all sports',
request: {
query: z.object({
search: z.string().optional(),
all: z.coerce.boolean().optional(),
...paginationSchema.shape,
}),
query: queryAllSchema,
},
responses: {
200: {
Expand Down
Loading

0 comments on commit 68b6960

Please sign in to comment.