diff --git a/src/controllers/SqueezeController.ts b/src/controllers/SqueezeController.ts index 38a95ebe..306f4199 100644 --- a/src/controllers/SqueezeController.ts +++ b/src/controllers/SqueezeController.ts @@ -10,7 +10,7 @@ class SqueezeController { /** * @openapi - * /api/v1/squeeze-pages: + * /api/v1/squeezes: * post: * tags: * - Squeeze @@ -158,6 +158,81 @@ class SqueezeController { }); } }; + + /** + * @openapi + * /api/v1/squeezes/{squeeze_id}: + * post: + * tags: + * - Squeeze + * summary: Update a squeeze page + * description: Update a squeeze entry. + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * properties: + * email: + * type: string + * first_name: + * type: string + * last_name: + * type: string + * phone: + * type: string + * location: + * type: string + * job_title: + * type: string + * company: + * type: string + * interests: + * type: array + * referral_source: + * type: string + * responses: + * 200: + * description: Squeeze updated successfully + * content: + * application/json: + * schema: + * type: object + * properties: + * status: + * type: string + * example: "success" + * message: + * type: string + * example: "Squeeze updated successfully" + * data: + * type: object + * 400: + * description: Bad request + * 404: + * description: Squeeze service not found + * 500: + * description: Error occurred while updating the squeeze record. + */ + public updateSqueeze = async (req: Request, res: Response) => { + const { squeeze_id } = req.params; + const data = req.body; + const squeeze = await this.squeezeService.updateSqueeze(squeeze_id, data); + + if (squeeze) { + return res.status(200).json({ + status: "Success", + message: "Squeeze updated successfully", + data: squeeze, + }); + } else { + res.status(500).json({ + status: "error", + message: "Error occurred while updating the squeeze record.", + }); + } + }; } export { SqueezeController }; diff --git a/src/controllers/jobController.ts b/src/controllers/jobController.ts index 920df706..73e5e999 100644 --- a/src/controllers/jobController.ts +++ b/src/controllers/jobController.ts @@ -73,7 +73,6 @@ export class JobController { * description: * type: string * example: "Develop and maintain software applications." - * // Add other job properties as needed * 404: * description: Job not found * content: @@ -88,7 +87,7 @@ export class JobController { * type: integer * example: 404 * 422: - * description: Validation failed: Valid job ID required + * description: Validation failed. Valid job ID required * content: * application/json: * schema: @@ -96,7 +95,7 @@ export class JobController { * properties: * message: * type: string - * example: "Validation failed: Valid job ID required" + * example: "Validation failed. Valid job ID required" * status_code: * type: integer * example: 422 diff --git a/src/routes/squeeze.ts b/src/routes/squeeze.ts index ffdc12ca..ea5cfc7d 100644 --- a/src/routes/squeeze.ts +++ b/src/routes/squeeze.ts @@ -6,7 +6,7 @@ const squeezeRoute = Router(); const squeezecontroller = new SqueezeController(); squeezeRoute.post( - "/squeeze-pages", + "/squeezes", authMiddleware, squeezecontroller.createSqueeze.bind(squeezecontroller), ); @@ -17,4 +17,10 @@ squeezeRoute.get( squeezecontroller.getSqueezeById.bind(squeezecontroller), ); +squeezeRoute.put( + "/squeezes/:squeeze_id", + authMiddleware, + squeezecontroller.updateSqueeze.bind(squeezecontroller), +); + export { squeezeRoute }; diff --git a/src/services/squeezeService.ts b/src/services/squeezeService.ts index b41ca5b3..2f7dea69 100644 --- a/src/services/squeezeService.ts +++ b/src/services/squeezeService.ts @@ -1,6 +1,11 @@ import { Squeeze } from "../models"; import AppDataSource from "../data-source"; -import { Conflict, BadRequest } from "../middleware"; +import { + Conflict, + BadRequest, + ResourceNotFound, + ServerError, +} from "../middleware"; import { squeezeSchema } from "../schema/squeezeSchema"; import { Repository } from "typeorm"; @@ -47,6 +52,28 @@ class SqueezeService { throw new BadRequest("Failed to retrieve squeeze: " + error.message); } } + + public async updateSqueeze(id: string, data: Squeeze): Promise { + const validation = squeezeSchema.safeParse(data); + if (!validation.success) { + throw new Conflict( + "Validation failed: " + + validation.error.errors.map((e) => e.message).join(", "), + ); + } + try { + const findSqueeze = await this.squeezeRepository.findOne({ + where: { id }, + }); + if (!findSqueeze) { + throw new ResourceNotFound("Squeeze not found"); + } + const newSqueeze = this.squeezeRepository.merge(findSqueeze, data); + return await this.squeezeRepository.save(newSqueeze); + } catch (error) { + throw new ServerError(error.message); + } + } } export { SqueezeService }; diff --git a/src/test/squeeze.spect.ts b/src/test/squeeze.spect.ts index bfeb90c7..88ab7480 100644 --- a/src/test/squeeze.spect.ts +++ b/src/test/squeeze.spect.ts @@ -1,6 +1,10 @@ +//@ts-nocheck import express from "express"; import request from "supertest"; import { Router } from "express"; +import { SqueezeService } from "../services"; +import { Squeeze } from "../models"; +import { ResourceNotFound } from "../middleware"; const mockSqueezeService = { getSqueezeById: jest.fn(),