diff --git a/__test__/controllers/jobs/createJob.controller.test.ts b/__test__/controllers/jobs/createJob.controller.test.ts index 5309121..ab3a050 100644 --- a/__test__/controllers/jobs/createJob.controller.test.ts +++ b/__test__/controllers/jobs/createJob.controller.test.ts @@ -4,7 +4,7 @@ import { type Queue } from "bullmq"; import { queueByName } from "../../../src/queues"; import createJob from "../../../src/controllers/jobs/createJob.controller"; -console.error = jest.fn() +console.error = jest.fn(); // Mock the queueByName function jest.mock("../../../src/queues", () => ({ diff --git a/__test__/controllers/jobs/getJob.controller.test.ts b/__test__/controllers/jobs/getJob.controller.test.ts index c8b06ec..d13662e 100644 --- a/__test__/controllers/jobs/getJob.controller.test.ts +++ b/__test__/controllers/jobs/getJob.controller.test.ts @@ -22,7 +22,7 @@ describe("getJob", () => { beforeEach(() => { req = { - params: {} + params: {}, } as Request; res = { json: jest.fn(), @@ -105,6 +105,8 @@ describe("getJob", () => { await getJob(req, res); expect(res.status).toHaveBeenCalledWith(500); - expect(res.json).toHaveBeenCalledWith({ error: "Failed to get job status" }); + expect(res.json).toHaveBeenCalledWith({ + error: "Failed to get job status", + }); }); }); diff --git a/__test__/index.test.ts b/__test__/index.test.ts index 5e5d7a1..35baced 100644 --- a/__test__/index.test.ts +++ b/__test__/index.test.ts @@ -10,7 +10,7 @@ console.log = jest.fn(); // Mock mongoose.connect and app.listen jest.mock("mongoose", () => ({ set: jest.fn(), - connect: jest.fn() + connect: jest.fn(), })); jest.mock("../src/app", () => ({ listen: jest.fn(), @@ -29,7 +29,7 @@ describe("main", () => { }); it("should connect to MongoDB and start the server", async () => { - await expect(require("../src/index").main()).resolves.not.toThrow() + await expect(require("../src/index").main()).resolves.not.toThrow(); expect(mongoose.set).toHaveBeenCalledWith("strictQuery", true); expect(mongoose.connect).toHaveBeenCalledWith("mocked-mongodb-url"); @@ -38,8 +38,8 @@ describe("main", () => { }); it("should handle errors and log them", async () => { - const error = new Error("Connection failed") - mongoose.connect = jest.fn().mockRejectedValueOnce(error) + const error = new Error("Connection failed"); + mongoose.connect = jest.fn().mockRejectedValueOnce(error); await expect(require("../src/index").main()).rejects.toThrow(error); expect(mongoose.connect).toHaveBeenCalledWith("mocked-mongodb-url"); diff --git a/__test__/jobs/email.job.test.ts b/__test__/jobs/email.job.test.ts index d9df9bf..6a4c1ab 100644 --- a/__test__/jobs/email.job.test.ts +++ b/__test__/jobs/email.job.test.ts @@ -1,13 +1,13 @@ -import { type Job } from 'bullmq'; -import emailJob from '../../src/jobs/email.job'; +import { type Job } from "bullmq"; +import emailJob from "../../src/jobs/email.job"; -describe('emailJob', () => { +describe("emailJob", () => { let consoleLogSpy: jest.SpyInstance; let consoleErrorSpy: jest.SpyInstance; beforeEach(() => { - consoleLogSpy = jest.spyOn(console, 'log'); - consoleErrorSpy = jest.spyOn(console, 'error'); + consoleLogSpy = jest.spyOn(console, "log"); + consoleErrorSpy = jest.spyOn(console, "error"); }); afterEach(() => { @@ -15,25 +15,26 @@ describe('emailJob', () => { consoleErrorSpy.mockRestore(); }); - it('should process email job successfully', async () => { + it("should process email job successfully", async () => { const jobData = { - to: 'example@example.com', - subject: 'Test Subject', - body: 'Test Body', + to: "example@example.com", + subject: "Test Subject", + body: "Test Body", }; const job: Partial = { - id: 'jobId', + id: "jobId", data: jobData, - name: 'emailJob', + name: "emailJob", }; await emailJob(job as Job); - expect(consoleLogSpy).toHaveBeenCalledWith(`Sending email to ${jobData.to}: ${jobData.subject}`); - expect(consoleLogSpy).toHaveBeenCalledWith('Body:', jobData.body); - expect(consoleLogSpy).toHaveBeenCalledWith('Email sent successfully'); + expect(consoleLogSpy).toHaveBeenCalledWith( + `Sending email to ${jobData.to}: ${jobData.subject}` + ); + expect(consoleLogSpy).toHaveBeenCalledWith("Body:", jobData.body); + expect(consoleLogSpy).toHaveBeenCalledWith("Email sent successfully"); expect(consoleErrorSpy).not.toHaveBeenCalled(); }); - }); diff --git a/__test__/jobs/imageProcessing.job.test.ts b/__test__/jobs/imageProcessing.job.test.ts index f35ce4e..e3ad7a0 100644 --- a/__test__/jobs/imageProcessing.job.test.ts +++ b/__test__/jobs/imageProcessing.job.test.ts @@ -1,13 +1,13 @@ -import { type Job } from 'bullmq'; -import imageProcessingJob from '../../src/jobs/imageProcessing.job'; +import { type Job } from "bullmq"; +import imageProcessingJob from "../../src/jobs/imageProcessing.job"; -describe('imageProcessingJob', () => { - it('should process the image successfully', async () => { - const imageUrl = 'https://example.com/image.jpg'; +describe("imageProcessingJob", () => { + it("should process the image successfully", async () => { + const imageUrl = "https://example.com/image.jpg"; const jobData = { imageUrl }; const job: Partial = { - id: 'job-id', - name: 'image-processing', + id: "job-id", + name: "image-processing", data: jobData, // Mock other attributes as needed }; diff --git a/__test__/workers/email.worker.test.ts b/__test__/workers/email.worker.test.ts index 85ad882..46e9d9d 100644 --- a/__test__/workers/email.worker.test.ts +++ b/__test__/workers/email.worker.test.ts @@ -1,28 +1,28 @@ -import { Worker } from 'bullmq' -import { emailQueue } from '../../src/queues'; -import connection from '../../src/queues/connection'; +import { Worker } from "bullmq"; +import { emailQueue } from "../../src/queues"; +import connection from "../../src/queues/connection"; -jest.mock('bullmq', () => ({ +jest.mock("bullmq", () => ({ ...jest.requireActual("bullmq"), - Worker: jest.fn().mockImplementation((queueName, processingFunction, options) => { - const workerMock = { - on: jest.fn(), - }; - return workerMock; - }), + Worker: jest + .fn() + .mockImplementation((queueName, processingFunction, options) => { + const workerMock = { + on: jest.fn(), + }; + return workerMock; + }), })); -describe('email.worker', () => { +describe("email.worker", () => { afterEach(() => { jest.clearAllMocks(); }); - it('should create a new BullMQ worker for emailQueue', async () => { - require('../../src/workers/email.worker') - expect(Worker).toHaveBeenCalledWith( - emailQueue.name, - expect.any(Function), - { connection } - ); + it("should create a new BullMQ worker for emailQueue", async () => { + require("../../src/workers/email.worker"); + expect(Worker).toHaveBeenCalledWith(emailQueue.name, expect.any(Function), { + connection, + }); }); }); diff --git a/src/app.ts b/src/app.ts index c46c8b8..bc560b7 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2,8 +2,8 @@ import express, { type Express, type Request, type Response } from "express"; import routes from "./routes"; const app: Express = express(); -app.use(express.json()) -app.use(routes) +app.use(express.json()); +app.use(routes); app.get("/", (req: Request, res: Response) => { res.send("Express + TypeScript Server"); diff --git a/src/controllers/jobs/createJob.controller.ts b/src/controllers/jobs/createJob.controller.ts index 002ee3a..b603e40 100644 --- a/src/controllers/jobs/createJob.controller.ts +++ b/src/controllers/jobs/createJob.controller.ts @@ -7,11 +7,11 @@ const createJob = async function (req: Request, res: Response) { try { const { type, data } = req.body; // Assuming you send the job type and data in the request body - let queue: Queue + let queue: Queue; try { - queue = queueByName(type) + queue = queueByName(type); } catch (error) { - console.error(error) + console.error(error); return res.status(400).json({ error }); } @@ -19,9 +19,9 @@ const createJob = async function (req: Request, res: Response) { return res.json({ jobId: job.id }); } catch (error) { - console.error('Error creating job:', error); - return res.status(500).json({ error: 'Failed to create job' }); + console.error("Error creating job:", error); + return res.status(500).json({ error: "Failed to create job" }); } -} +}; -export default createJob \ No newline at end of file +export default createJob; diff --git a/src/controllers/jobs/getJob.controller.ts b/src/controllers/jobs/getJob.controller.ts index bbed605..92105e7 100644 --- a/src/controllers/jobs/getJob.controller.ts +++ b/src/controllers/jobs/getJob.controller.ts @@ -7,11 +7,11 @@ const getJob = async function (req: Request, res: Response) { try { const { type, jobId } = req.params; // Assuming the jobId is passed as a route parameter - let queue: Queue + let queue: Queue; try { - queue = queueByName(type) + queue = queueByName(type); } catch (error) { - console.error('Error getting job queue:', error) + console.error("Error getting job queue:", error); return res.status(400).json({ error }); } @@ -19,7 +19,7 @@ const getJob = async function (req: Request, res: Response) { const job = await Job.fromId(queue, jobId); if (job == null) { - return res.status(404).json({ error: 'Job not found' }); + return res.status(404).json({ error: "Job not found" }); } return res.json({ @@ -30,9 +30,9 @@ const getJob = async function (req: Request, res: Response) { data: job.data, }); } catch (error) { - console.error('Error getting job status:', error); - return res.status(500).json({ error: 'Failed to get job status' }); + console.error("Error getting job status:", error); + return res.status(500).json({ error: "Failed to get job status" }); } -} +}; -export default getJob \ No newline at end of file +export default getJob; diff --git a/src/controllers/jobs/index.ts b/src/controllers/jobs/index.ts index eecf4f7..6e05d85 100644 --- a/src/controllers/jobs/index.ts +++ b/src/controllers/jobs/index.ts @@ -1,7 +1,4 @@ import createJob from "./createJob.controller"; import getJob from "./getJob.controller"; -export { - createJob, - getJob -} \ No newline at end of file +export { createJob, getJob }; diff --git a/src/index.ts b/src/index.ts index d088cf3..cb5d8c3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import mongoose from "mongoose"; import app from "./app"; import { env } from "./config"; -import './workers' +import "./workers"; mongoose.set("strictQuery", true); diff --git a/src/jobs/email.job.ts b/src/jobs/email.job.ts index dfd6b00..5dc8cde 100644 --- a/src/jobs/email.job.ts +++ b/src/jobs/email.job.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/restrict-template-expressions */ -import { type Job } from 'bullmq'; +import { type Job } from "bullmq"; const emailJob = async (job: Job): Promise => { // Define the processing logic for email jobs @@ -8,16 +8,16 @@ const emailJob = async (job: Job): Promise => { try { // Perform email processing logic here console.log(`Sending email to ${to}: ${subject}`); - console.log('Body:', body); + console.log("Body:", body); // Simulating email sending time await new Promise((resolve) => setTimeout(resolve, 2000)); - console.log('Email sent successfully'); + console.log("Email sent successfully"); } catch (error) { - console.error('Error processing email:', error); - throw new Error('Failed to process email'); // Throw an error if email processing fails + console.error("Error processing email:", error); + throw new Error("Failed to process email"); // Throw an error if email processing fails } -} +}; -export default emailJob \ No newline at end of file +export default emailJob; diff --git a/src/jobs/imageProcessing.job.ts b/src/jobs/imageProcessing.job.ts index e1ec804..3da8ff3 100644 --- a/src/jobs/imageProcessing.job.ts +++ b/src/jobs/imageProcessing.job.ts @@ -12,11 +12,11 @@ const imageProcessingJob = async function (job: Job): Promise { // Simulating image processing time await new Promise((resolve) => setTimeout(resolve, 3000)); - console.log('Image processing completed'); + console.log("Image processing completed"); } catch (error) { - console.error('Error processing image:', error); - throw new Error('Failed to process image'); // Throw an error if image processing fails + console.error("Error processing image:", error); + throw new Error("Failed to process image"); // Throw an error if image processing fails } -} +}; -export default imageProcessingJob \ No newline at end of file +export default imageProcessingJob; diff --git a/src/queues/connection.ts b/src/queues/connection.ts index 2ae423b..f741e04 100644 --- a/src/queues/connection.ts +++ b/src/queues/connection.ts @@ -3,7 +3,7 @@ import { env } from "../config"; const connection: RedisOptions = { host: env.REDIS_QUEUE_HOST, - port: env.REDIS_QUEUE_PORT -} + port: env.REDIS_QUEUE_PORT, +}; -export default connection \ No newline at end of file +export default connection; diff --git a/src/queues/email.queue.ts b/src/queues/email.queue.ts index 505da4b..7e063d2 100644 --- a/src/queues/email.queue.ts +++ b/src/queues/email.queue.ts @@ -1,6 +1,6 @@ import { Queue } from "bullmq"; import connection from "./connection"; -const emailQueue = new Queue('email', { connection }) +const emailQueue = new Queue("email", { connection }); -export default emailQueue; \ No newline at end of file +export default emailQueue; diff --git a/src/queues/imageProcessing.queue.ts b/src/queues/imageProcessing.queue.ts index 7d44a5a..6abb770 100644 --- a/src/queues/imageProcessing.queue.ts +++ b/src/queues/imageProcessing.queue.ts @@ -1,6 +1,6 @@ import { Queue } from "bullmq"; import connection from "./connection"; -const imageProcessingQueue = new Queue('imageProcessing', { connection }) +const imageProcessingQueue = new Queue("imageProcessing", { connection }); -export default imageProcessingQueue; \ No newline at end of file +export default imageProcessingQueue; diff --git a/src/queues/index.ts b/src/queues/index.ts index 4de6dfd..e077c6d 100644 --- a/src/queues/index.ts +++ b/src/queues/index.ts @@ -1,9 +1,5 @@ import emailQueue from "./email.queue"; -import imageProcessingQueue from "./imageProcessing.queue" +import imageProcessingQueue from "./imageProcessing.queue"; import queueByName from "./queueByName"; -export { - emailQueue, - imageProcessingQueue, - queueByName -} \ No newline at end of file +export { emailQueue, imageProcessingQueue, queueByName }; diff --git a/src/queues/queueByName.ts b/src/queues/queueByName.ts index 3a8a2de..c1f98ba 100644 --- a/src/queues/queueByName.ts +++ b/src/queues/queueByName.ts @@ -3,14 +3,14 @@ import emailQueue from "./email.queue"; import imageProcessingQueue from "./imageProcessing.queue"; const queueByName = (name: string): Queue => { - switch (name) { - case emailQueue.name: - return emailQueue; - case imageProcessingQueue.name: - return imageProcessingQueue; - default: - throw new Error(`No Queue called ${name}`) - } -} + switch (name) { + case emailQueue.name: + return emailQueue; + case imageProcessingQueue.name: + return imageProcessingQueue; + default: + throw new Error(`No Queue called ${name}`); + } +}; -export default queueByName \ No newline at end of file +export default queueByName; diff --git a/src/routes/index.ts b/src/routes/index.ts index 8f81d99..0ceb73f 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,10 +1,10 @@ import express from "express"; -import jobsRoute from "./jobs.route" -import postsRoute from "./posts.route" +import jobsRoute from "./jobs.route"; +import postsRoute from "./posts.route"; const router = express.Router(); -router.use("/jobs", jobsRoute) -router.use("/posts", postsRoute) +router.use("/jobs", jobsRoute); +router.use("/posts", postsRoute); -export default router \ No newline at end of file +export default router; diff --git a/src/routes/jobs.route.ts b/src/routes/jobs.route.ts index 90a806f..4777479 100644 --- a/src/routes/jobs.route.ts +++ b/src/routes/jobs.route.ts @@ -5,6 +5,6 @@ import { createJob, getJob } from "../controllers/jobs"; const router = express.Router(); router.post("/", createJob); -router.get("/:type/:jobId", getJob) +router.get("/:type/:jobId", getJob); export default router; diff --git a/src/workers/email.worker.ts b/src/workers/email.worker.ts index 877232a..a548f95 100644 --- a/src/workers/email.worker.ts +++ b/src/workers/email.worker.ts @@ -1,10 +1,10 @@ /* eslint-disable @typescript-eslint/restrict-template-expressions */ -import { Worker } from 'bullmq'; -import { emailQueue } from '../queues'; // Assuming you have already defined the emailQueue -import connection from '../queues/connection'; -import emailJob from '../jobs/email.job'; +import { Worker } from "bullmq"; +import { emailQueue } from "../queues"; // Assuming you have already defined the emailQueue +import connection from "../queues/connection"; +import emailJob from "../jobs/email.job"; // Create a new BullMQ worker instance for emailQueue const emailWorker = new Worker(emailQueue.name, emailJob, { connection }); -export default emailWorker \ No newline at end of file +export default emailWorker; diff --git a/src/workers/imageProcessing.worker.ts b/src/workers/imageProcessing.worker.ts index 90f6648..f8fb016 100644 --- a/src/workers/imageProcessing.worker.ts +++ b/src/workers/imageProcessing.worker.ts @@ -1,10 +1,14 @@ /* eslint-disable @typescript-eslint/restrict-template-expressions */ -import { Worker } from 'bullmq'; -import { imageProcessingQueue } from '../queues'; // Assuming you have already defined the imageProcessingQueue -import connection from '../queues/connection'; -import imageProcessingJob from '../jobs/imageProcessing.job'; +import { Worker } from "bullmq"; +import { imageProcessingQueue } from "../queues"; // Assuming you have already defined the imageProcessingQueue +import connection from "../queues/connection"; +import imageProcessingJob from "../jobs/imageProcessing.job"; // Create a new BullMQ worker instance for imageProcessingQueue -const imageProcessingWorker = new Worker(imageProcessingQueue.name, imageProcessingJob, { connection }); +const imageProcessingWorker = new Worker( + imageProcessingQueue.name, + imageProcessingJob, + { connection } +); -export default imageProcessingWorker \ No newline at end of file +export default imageProcessingWorker; diff --git a/src/workers/index.ts b/src/workers/index.ts index d614c9a..81d1d0e 100644 --- a/src/workers/index.ts +++ b/src/workers/index.ts @@ -1,7 +1,4 @@ -import emailWorker from './email.worker' -import imageProcessingWorker from './imageProcessing.worker' +import emailWorker from "./email.worker"; +import imageProcessingWorker from "./imageProcessing.worker"; -export { - emailWorker, - imageProcessingWorker -} \ No newline at end of file +export { emailWorker, imageProcessingWorker };