Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions backend/src/controllers/jobController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,20 @@ export const updatePublishStatus = async (req: Request, res: Response) => {
status: 'success',
});
};

export const deleteJob = async (req: Request, res: Response) => {
const { companyId, jobId } = req.params;
const deleteJob = await jobServices.deleteJob(companyId, jobId);

if (!deleteJob) {
res
.status(404)
.json({ message: 'No job found with this id', status: 'fail' });
return;
}

res.status(200).json({
message: 'Job deletion successful',
status: 'success',
});
};
4 changes: 3 additions & 1 deletion backend/src/models/job.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { model, Schema } from 'mongoose';
import type { Job } from '@/schemas';
import { EmploymentType, WorkPlaceType, JobStatus } from '@/types/index';
import softDeletePlugin from '@/plugins/softDelete.plugin';

const JobSchema = new Schema<Job>(
{
Expand All @@ -22,9 +23,10 @@ const JobSchema = new Schema<Job>(
vacancy: { type: Number, required: true },
tags: [{ type: String }],
status: { type: String, enum: JobStatus, default: 'draft' },
deletedAt: { type: Date, default: null },
},
{ timestamps: true, virtuals: true },
);

JobSchema.plugin(softDeletePlugin);

export const JobModel = model('Job', JobSchema);
17 changes: 17 additions & 0 deletions backend/src/plugins/softDelete.plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Schema } from 'mongoose';

export default function softDeletePlugin(schema: Schema) {
schema.add({
deletedAt: { type: Date, default: null },
});

schema.methods.softDelete = async function () {
this.deletedAt = new Date();
await this.save();
};

schema.methods.restore = async function () {
this.deletedAt = null;
await this.save();
};
}
1 change: 1 addition & 0 deletions backend/src/routes/jobRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ router.patch(
validatePayload(UpdateJobStatusSchema),
jobController.updatePublishStatus,
);
router.delete('/:companyId/jobs/:jobId', jobController.deleteJob);

export default router;
13 changes: 13 additions & 0 deletions backend/src/services/jobServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,16 @@ export const updateJobStatus = async (

return updateJobStatus;
};

export const deleteJob = async (companyId: string, jobId: string) => {
const findJob = await JobModel.findOne({ company: companyId, _id: jobId });

if (!findJob || findJob?.deletedAt) {
return false;
}

// @ts-expect-error: softDelete is added by the Mongoose plugin, not in the type definition
await findJob.softDelete();

return true;
};