Skip to content

Commit f962bf2

Browse files
committed
error-in-test
1 parent 1777c3d commit f962bf2

8 files changed

Lines changed: 50 additions & 51 deletions

File tree

src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ app.use(json())
3030
app.use(urlencoded({ extended: true }))
3131

3232
// 3) Handle file uploads (in-memory)
33-
const upload = multer({ storage: multer.memoryStorage() })
33+
const upload = multer({ storage: multer.memoryStorage() })
3434

3535
// 4) Mount your routes, injecting `upload` middleware where needed
3636
// For endpoints that accept file uploads, you can do e.g.:

src/controllers/project.controller.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as projectService from "../services/project.service";
22
import { Request , Response } from "express";
33
import { ApiError } from "../utils/apiError";
44
import { error } from "console";
5+
import { uploadImage } from "../utils/imageUtils";
6+
import { supabase } from "../app";
57

68
export const getProjects = async ( req : Request , res : Response ) => {
79

@@ -31,15 +33,27 @@ export const getProjectById = async ( req : Request , res : Response ) => {
3133
export const createProject = async ( req : Request , res : Response ) => {
3234

3335
try {
34-
const projectContent = req.body;
35-
const AdminId = req.body.AdminId;
36-
if( projectContent.name.length === 0 || !projectContent.githubUrl || !AdminId || !projectContent.imageUrl) throw new ApiError( " Field is missing !!!" , 400);
36+
const file = req.file;
37+
if(!file) throw new ApiError('Image file is not find' , 400);
3738

38-
const project = await projectService.createProject(projectContent , AdminId);
39-
res.status(200).json(project);
39+
const imageUrl = await uploadImage( supabase , file , 'projects');
40+
41+
if(!imageUrl) throw new ApiError("Image url is missing" , 400);
42+
43+
const projectContent = {
44+
name: req.body.projectData.project,
45+
imageUrl: imageUrl,
46+
githubUrl: req.body.projectData.githubUrl,
47+
deployUrl: req.body.projectData.deployUrl,
48+
AdminId: req.body.projectData.AdminId,
49+
};
50+
51+
const project = await projectService.createProject( projectContent );
52+
res.status(200).json(project);
4053

4154
} catch (error) {
42-
throw new ApiError( error as string , 500);
55+
console.log(error);
56+
throw new ApiError( error as string , 404);
4357
}
4458
};
4559

src/routes/index.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,3 @@ export default function routes(upload: Multer, supabase: SupabaseClient) {
1515
return router
1616
}
1717

18-
/*
19-
write your routes like this, skip the paramaeters in the exported function if your routes don't need supabase and multer
20-
21-
import { Router } from 'express'
22-
import { Multer } from 'multer'
23-
import * as memberCtrl from '../controllers/member.controller'
24-
import { SupabaseClient } from '@supabase/supabase-js'
25-
26-
export default function membersRouter(
27-
upload: Multer,
28-
supabase: SupabaseClient
29-
) {
30-
const router = Router()
31-
32-
// Photo upload endpoint:
33-
router.post(
34-
'/:memberId/photo',
35-
upload.single('photo'),
36-
(req, res, next) => memberCtrl.uploadPhoto(req, res, next, supabase)
37-
)
38-
39-
return router
40-
}
41-
42-
*/

src/routes/projects.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Router } from 'express'
2-
import { Multer } from 'multer'
1+
import { NextFunction, Request, Response, Router } from 'express'
2+
import { Multer } from 'multer'
33
import { SupabaseClient } from '@supabase/supabase-js'
44
import {
55
addMembers,
@@ -13,6 +13,18 @@ import {
1313
} from '../controllers/project.controller'
1414

1515

16+
function parseCreateProjectData(req : Request, res : Response , next : NextFunction) {
17+
18+
if(req.body.projectData){
19+
try{
20+
const parse = JSON.parse(req.body.projectData);
21+
req.body = parse;
22+
}catch(e){
23+
return res.status(400).json({ message: 'Invalid JSON in projectData field' });
24+
}
25+
}
26+
next();
27+
}
1628

1729
export default function projectsRouter(
1830
upload: Multer,
@@ -60,7 +72,7 @@ export default function projectsRouter(
6072
* @apiError { error 400} some fields are missing
6173
* @apiError { error 500} error related to the db interaction
6274
*/
63-
router.post('/create', createProject )
75+
router.post('/create', upload.single('image') , parseCreateProjectData , createProject )
6476

6577
// Update Project
6678
/**
@@ -150,12 +162,6 @@ export default function projectsRouter(
150162

151163
router.delete( '/:projectId/members/:memberId', removeMembers)
152164

153-
// Photo upload endpoint:
154-
// router.post(
155-
// '/:projectId/photo',
156-
// upload.single('photo'),
157-
// (req, res, next) => memberCtrl.uploadPhoto(req, res, next, supabase)
158-
// )
159165

160166

161167
return router

src/services/project.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ export const getProjectById = async ( projectId : number ) => {
1616
})
1717
};
1818

19-
export const createProject = async ( projectContent : projectContent , AdminId : string) => {
19+
export const createProject = async ( projectContent : projectContent ) => {
2020

2121
return await prisma.project.create({
2222
data : {
2323
name : projectContent.name,
2424
imageUrl : projectContent.imageUrl,
2525
githubUrl : projectContent.githubUrl,
2626
deployUrl : projectContent.deployUrl,
27-
createdById : AdminId,
27+
createdById : projectContent.AdminId,
2828
},
2929
})
3030
};

src/types/project.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ declare global {
88
name: string,
99
imageUrl: string ,
1010
githubUrl: string,
11-
deployUrl: string?
11+
deployUrl: string?,
12+
AdminId : string
1213
}
1314

1415
type addMembersData = {

tests/Project.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { describe } from 'node:test';
2-
import { createProject , getProjectById , updateProjects , deleteProjects , getMembersByProjectId , addMembers , removeMembers} from '../src/controllers/project.controller';
1+
import { createProject , getProjectById , updateProjects , deleteProjects
2+
, getMembersByProjectId , addMembers , removeMembers} from '../src/controllers/project.controller';
33
import * as projectService from '../src/services/project.service';
44
import { ApiError } from '../src/utils/apiError';
5+
import { Response , Request} from 'express';
56

67
// modify the respose Object
78

89
const mockRes = () => {
9-
const res: any = {};
10+
const res = {} as Response;
1011
res.status = jest.fn().mockReturnValue(res);
1112
res.json = jest.fn().mockReturnValue(res);
1213
return res;
@@ -20,16 +21,18 @@ describe('createProjectHandler', () => {
2021

2122

2223
it('should return 200 and created project with valid input', async () => {
23-
const req: any = {
24+
const req = {
2425
body: {
2526
name: 'EventHub',
26-
imageUrl: 'https://example.com/image.png',
2727
githubUrl: 'https://github.com/example/eventhub',
2828
deployUrl: 'https://eventhub.example.com',
2929
AdminId,
3030
},
31+
file : {
32+
name : "image"
33+
}
3134

32-
};
35+
} as unknown as Request;
3336

3437
const res = mockRes();
3538

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
111111
"skipLibCheck": true /* Skip type checking all .d.ts files. */
112112
},
113-
"include": ["src/**/*"], // Include your source files
113+
"include": ["src/**/*", "tests/imageUtils.test.ts"], // Include your source files
114114
"exclude": ["node_modules", "tests/**/*"] // Exclude test files from compilation
115115
}
116116

0 commit comments

Comments
 (0)