Skip to content

Commit

Permalink
Merge pull request #534 from PreciousIfeaka/feat/update-squeeze
Browse files Browse the repository at this point in the history
feat: update squeeze page by ID
  • Loading branch information
AdeGneus authored Aug 8, 2024
2 parents 6298165 + c9a46a5 commit e8bec72
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 6 deletions.
77 changes: 76 additions & 1 deletion src/controllers/SqueezeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SqueezeController {

/**
* @openapi
* /api/v1/squeeze-pages:
* /api/v1/squeezes:
* post:
* tags:
* - Squeeze
Expand Down Expand Up @@ -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 };
5 changes: 2 additions & 3 deletions src/controllers/jobController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -88,15 +87,15 @@ 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:
* type: object
* properties:
* message:
* type: string
* example: "Validation failed: Valid job ID required"
* example: "Validation failed. Valid job ID required"
* status_code:
* type: integer
* example: 422
Expand Down
8 changes: 7 additions & 1 deletion src/routes/squeeze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const squeezeRoute = Router();
const squeezecontroller = new SqueezeController();

squeezeRoute.post(
"/squeeze-pages",
"/squeezes",
authMiddleware,
squeezecontroller.createSqueeze.bind(squeezecontroller),
);
Expand All @@ -17,4 +17,10 @@ squeezeRoute.get(
squeezecontroller.getSqueezeById.bind(squeezecontroller),
);

squeezeRoute.put(
"/squeezes/:squeeze_id",
authMiddleware,
squeezecontroller.updateSqueeze.bind(squeezecontroller),
);

export { squeezeRoute };
29 changes: 28 additions & 1 deletion src/services/squeezeService.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -47,6 +52,28 @@ class SqueezeService {
throw new BadRequest("Failed to retrieve squeeze: " + error.message);
}
}

public async updateSqueeze(id: string, data: Squeeze): Promise<Squeeze> {
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 };
4 changes: 4 additions & 0 deletions src/test/squeeze.spect.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down

0 comments on commit e8bec72

Please sign in to comment.