-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from atlp-rwanda/ft-order-management
ft(order): A buyer and seller should be able to watch their orders
- Loading branch information
Showing
13 changed files
with
372 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Request, Response } from "express" | ||
import { getOrderBuyer, updateOrderStatusService } from "../services/OrderService" | ||
import * as mailService from "../services/mail.service"; | ||
import { SUBJECTS, UserId } from "../types"; | ||
import { orderStatusTemplate } from "../email-templates/orderStatus"; | ||
import Order from "../sequelize/models/orders"; | ||
import User from "../sequelize/models/users"; | ||
|
||
|
||
export const getOrderController = async (req: Request, res: Response) => { | ||
try { | ||
const response = await getOrderBuyer(req, res) | ||
return res.status(200).json({ | ||
status: 200, | ||
length: response?.length, | ||
orders: response?.length === 0? "You have not order yet" : response | ||
}) | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).json({ | ||
status: 500, | ||
message: `error accured during fetching order ${error}` | ||
}) | ||
} | ||
} | ||
|
||
export const updateOrderStatus = async (req: Request, res: Response) => { | ||
|
||
try { | ||
const { id: userId} = req.user as UserId; | ||
const { id: orderId } = req.params; | ||
const { status } = req.body; | ||
const buyerId = await Order.findByPk(orderId) | ||
const dataValues = await User.findByPk(buyerId?.buyerId) | ||
|
||
const updatedItems = await updateOrderStatusService(userId, orderId, status); | ||
if (updatedItems) { | ||
// @ts-ignore | ||
await mailService.sendNotification(dataValues?.dataValues.email, SUBJECTS.ORDER_STATUS_UPDATED, orderStatusTemplate(dataValues?.dataValues.username, buyerId?.id, buyerId?.createdAt, status)) | ||
|
||
return res.json({ message: 'Order items status updated', updatedItems }); | ||
} | ||
|
||
} catch (error: any) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { required } from "joi"; | ||
|
||
export const StatusSchema = { | ||
type: "object", | ||
properties: { | ||
status: { | ||
type: "string", | ||
}, | ||
}, | ||
}; | ||
|
||
export const buyerAndSellerOrder = { | ||
tags: ["orders"], | ||
security: [{ bearerAuth: [] }], | ||
summary: "Orders for seller and buyers", | ||
responses: { | ||
200: { | ||
description: "Success", | ||
}, | ||
400: { | ||
description: "Bad request", | ||
}, | ||
401: { | ||
description: "Unauthorized", | ||
}, | ||
500: { | ||
description: "Internal server error", | ||
}, | ||
}, | ||
}; | ||
|
||
export const statusUpdate = { | ||
tags: ["orders"], | ||
security: [{ bearerAuth: [] }], | ||
summary: "Status update for seller", | ||
parameters: [ | ||
{ | ||
name: "id", | ||
in: "path", | ||
required: true, | ||
description: "order Id", | ||
schema: { | ||
type: "number", | ||
}, | ||
}, | ||
], | ||
requestBody: { | ||
required: true, | ||
content: { | ||
"application/json": { | ||
schema: { | ||
type: "object", | ||
properties: { | ||
status: { | ||
type: "string", | ||
}, | ||
}, | ||
required: ["status"], | ||
}, | ||
}, | ||
}, | ||
}, | ||
responses: { | ||
201: { | ||
description: "Upgrade successfully", | ||
}, | ||
404: { | ||
description: "Review not found", | ||
}, | ||
500: { | ||
description: "Internal server error.", | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
export const orderStatusTemplate = (username: string, orderId: number, createdAt: Date, status: string) => { | ||
return ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Order Status</title> | ||
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&display=swap" rel="stylesheet"> | ||
<style> | ||
body { | ||
font-family: 'Poppins', sans-serif; | ||
background-color: #f8f9fa; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100vh; | ||
margin: 0; | ||
} | ||
.container { | ||
width: 80%; | ||
max-width: 400px; | ||
margin: auto; | ||
padding: 30px; | ||
background-color: #ffffff; | ||
border-radius: 8px; | ||
box-shadow: 0 2px 10px rgba(0,0,0,0.1); | ||
text-align: center; | ||
} | ||
.title { | ||
color: #333333; | ||
font-size: 24px; | ||
margin-bottom: 20px; | ||
} | ||
.content { | ||
color: #666666; | ||
font-size: 16px; | ||
line-height: 1.6; | ||
margin-bottom: 20px; | ||
} | ||
.status { | ||
color: black; | ||
} | ||
.footer { | ||
font-style: italic; | ||
color: #999999; | ||
} | ||
.status{ | ||
font-weigth: 800; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<h1 class="title">Order Status Updated 😍</h1> | ||
<p class="content">Dear ${username}, Your product status with this order id #${orderId} has been successfully to <span class="status"> ${status}</span> .</p> | ||
<div class="status">Status updated at ${createdAt}</div> <br /> | ||
<div class="footer">I hope you liked our services.</div> | ||
</div> | ||
</body> | ||
</html> | ||
`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import express from "express" | ||
import { getOrderController, updateOrderStatus, } from "../controllers/getOrderController"; | ||
import { isLoggedIn } from "../middlewares/isLoggedIn"; | ||
import { isAseller } from "../middlewares/sellerAuth"; | ||
|
||
|
||
const orderRouter = express.Router() | ||
|
||
orderRouter.get('/', isLoggedIn, getOrderController) | ||
orderRouter.patch("/:id/status", isLoggedIn, updateOrderStatus) | ||
|
||
export default orderRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/sequelize/migrations/x20240711191430-modify-order-items.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
return Promise.all([ | ||
queryInterface.addColumn( | ||
'orderItems', | ||
'userId', | ||
{ | ||
type: Sequelize.INTEGER, | ||
allowNull: true, | ||
}, | ||
), | ||
queryInterface.addColumn( | ||
'orderItems', | ||
'status', | ||
{ | ||
type: Sequelize.ENUM, | ||
allowNull: false, | ||
values: ["Pending", "Delivered", "Cancelled"], | ||
defaultValue: 'Pending' | ||
}, | ||
), | ||
]); | ||
}, | ||
|
||
down(queryInterface, Sequelize) { | ||
return Promise.all([ | ||
queryInterface.removeColumn('orderItems', 'status'), | ||
queryInterface.removeColumn('orderItems', 'userId'), | ||
]); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.