diff --git a/packages/apiGateway/src/config.json b/packages/apiGateway/src/config.json index 0d4d3ed..73cd373 100644 --- a/packages/apiGateway/src/config.json +++ b/packages/apiGateway/src/config.json @@ -60,8 +60,18 @@ } ] }, - "notification": { + "email" : { "url": "http://localhost:4003", + "routes": [ + { + "path": "/send-email", + "methods": ["post"], + "middlewares": [] + } + ] + }, + "notification": { + "url": "http://localhost:4004", "routes": [ { "path": "/users/:id", diff --git a/packages/services/auth/prisma/migrations/20240425144230_init/migration.sql b/packages/services/auth/prisma/migrations/20240428180722_init/migration.sql similarity index 100% rename from packages/services/auth/prisma/migrations/20240425144230_init/migration.sql rename to packages/services/auth/prisma/migrations/20240428180722_init/migration.sql diff --git a/packages/services/email/prisma/migrations/20240428182309_/migration.sql b/packages/services/email/prisma/migrations/20240428182309_/migration.sql new file mode 100644 index 0000000..943b016 --- /dev/null +++ b/packages/services/email/prisma/migrations/20240428182309_/migration.sql @@ -0,0 +1,13 @@ +-- CreateTable +CREATE TABLE "Email" ( + "id" TEXT NOT NULL, + "sender" TEXT NOT NULL DEFAULT '', + "recipient" TEXT NOT NULL, + "subject" TEXT NOT NULL, + "body" TEXT NOT NULL, + "source" TEXT NOT NULL DEFAULT '', + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "Email_pkey" PRIMARY KEY ("id") +); diff --git a/packages/services/email/prisma/migrations/migration_lock.toml b/packages/services/email/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..fbffa92 --- /dev/null +++ b/packages/services/email/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "postgresql" \ No newline at end of file diff --git a/packages/services/email/prisma/schema.prisma b/packages/services/email/prisma/schema.prisma index ee282c7..b5a7f83 100644 --- a/packages/services/email/prisma/schema.prisma +++ b/packages/services/email/prisma/schema.prisma @@ -12,3 +12,14 @@ datasource db { provider = "postgresql" url = env("DATABASE_URL") } + +model Email { + id String @id @default(cuid()) + sender String @default("") + recipient String + subject String + body String + source String @default("") + createdAt DateTime @default(now()) + updatedAt DateTime @default(now()) +} diff --git a/packages/services/email/src/controllers/getEmails.ts b/packages/services/email/src/controllers/getEmails.ts index e69de29..1dee0ff 100644 --- a/packages/services/email/src/controllers/getEmails.ts +++ b/packages/services/email/src/controllers/getEmails.ts @@ -0,0 +1,7 @@ +import { Request, Response,NextFunction } from "express" + +const getEmail = async (req: Request, res: Response, next: NextFunction) => { + +} + +export default getEmail \ No newline at end of file diff --git a/packages/services/email/src/controllers/sendEmail.ts b/packages/services/email/src/controllers/sendEmail.ts index b61160c..bf302cf 100644 --- a/packages/services/email/src/controllers/sendEmail.ts +++ b/packages/services/email/src/controllers/sendEmail.ts @@ -1,6 +1,6 @@ import { Request, Response, NextFunction } from 'express'; import prisma from '@/utils/prisma'; -import { EmailCreateSchema } from '@/utils/schemas'; +import { emailCreateSchema } from '@/utils/schemas'; import { defaultSender, transporter } from '@/config'; @@ -9,12 +9,56 @@ import { defaultSender, transporter } from '@/config'; const sendEmail = async (req: Request, res: Response, next: NextFunction) => { try { // validate input the req body - const parsedBody = EmailCreateSchema.safeParse(req.body) + const parsedBody = emailCreateSchema.safeParse(req.body) if (!parsedBody.success) { return res.status(400).json(parsedBody.error.errors) } + + // create a new email option + const { sender, recipient, subject, body, source } = parsedBody.data + const from = sender || defaultSender + + const emailOptions = { + from, + to: recipient, + subject, + text: body, + + } + + // send email + + const { rejected } = await transporter.sendMail(emailOptions) + if(rejected.length){ + console.log("Email not sent", rejected) + return res.status(500).json({ + message: "Email not sent", + rejected + }) + } + + await prisma.email.create({ + data: { + sender, + recipient, + subject, + body, + source + } + }) + + // return success + + return res.status(200).json({ + message: "Email sent" + + }) + + } catch (error) { next(error) } -} \ No newline at end of file +} + +export default sendEmail \ No newline at end of file diff --git a/packages/services/email/src/index.ts b/packages/services/email/src/index.ts index 6cd92c7..fe892eb 100644 --- a/packages/services/email/src/index.ts +++ b/packages/services/email/src/index.ts @@ -1,4 +1,33 @@ -import init from "./app"; +import express from "express"; +import dotenv from "dotenv"; +import cors from "cors"; +import morgan from "morgan"; +import error = require("@/utils/error"); +import router from "./routes"; -init() \ No newline at end of file +dotenv.config(); + +const app = express(); +app.use(express.json()); +app.use(cors()); +app.use(morgan("dev")); + +app.get("/health", (_req, res) => { + res.status(200).json({ status: "UP" }); +}); + +// routes + +app.use("/", router); + +// handler +app.use(error.notFound); +app.use(error.serverError); + +const port = process.env.PORT || 4000; +const serviceName = process.env.SERVICE_NAME || "Email-Service"; + +app.listen(port, () => { + console.log(`${serviceName} is running on port ${port}`); +}); diff --git a/packages/services/email/src/routes/index.ts b/packages/services/email/src/routes/index.ts new file mode 100644 index 0000000..13dd179 --- /dev/null +++ b/packages/services/email/src/routes/index.ts @@ -0,0 +1,9 @@ +import express from "express"; +const router = express.Router(); +import { getEmails, sendEmail } from '@/controllers' + + +router.post('/emails/send', sendEmail) +router.get('/emails', getEmails) + +export default router; diff --git a/packages/services/email/src/utils/index.ts b/packages/services/email/src/utils/index.ts deleted file mode 100644 index e69de29..0000000 diff --git a/packages/services/email/src/utils/schemas.ts b/packages/services/email/src/utils/schemas.ts index bde6438..24e9d8e 100644 --- a/packages/services/email/src/utils/schemas.ts +++ b/packages/services/email/src/utils/schemas.ts @@ -1,9 +1,9 @@ import { z } from 'zod'; -export const EmailCreateSchema = z.object({ - recipient: z.string().email(), +export const emailCreateSchema = z.object({ + sender: z.string().optional(), + recipient: z.string(), subject: z.string(), body: z.string(), - source: z.string(), - sender: z.string().email().optional(), + source: z.string().optional() }); \ No newline at end of file diff --git a/packages/services/notification/src/index.ts b/packages/services/notification/src/index.ts index 5602cd0..c2a88d6 100644 --- a/packages/services/notification/src/index.ts +++ b/packages/services/notification/src/index.ts @@ -17,6 +17,13 @@ app.use(express.urlencoded({ extended: true })); app.use(express.static("public")); +const port = process.env.PORT || 4004; + + +app.listen(port, () => { + console.log(`Notification service listening on port ${port}`); +}) + app.get("/", (_req, res) => { res.status(200).json({ status: "UP" }); }) diff --git a/packages/services/user/prisma/migrations/20240425143720_init/migration.sql b/packages/services/user/prisma/migrations/20240428181000_init/migration.sql similarity index 100% rename from packages/services/user/prisma/migrations/20240425143720_init/migration.sql rename to packages/services/user/prisma/migrations/20240428181000_init/migration.sql