Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: fixed undefined notification when product deleted #96

Merged
merged 2 commits into from
Jul 24, 2024
Merged
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
13 changes: 8 additions & 5 deletions src/controllers/productControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { updateProductTemplate } from "../email-templates/updated";
import { createdProductTemplate } from "../email-templates/created";
import { createReview, deleteReview, getProductReviews, updateReview } from "../services/product.service";
import Review from "../sequelize/models/reviews";
import Product from "../sequelize/models/products";

export const fetchProducts = async (req: Request, res: Response) => {
try {
Expand Down Expand Up @@ -162,18 +163,20 @@ export const productsUpdate = async (req: Request, res: Response) => {

export const removeProducts = async (req: Request, res: Response) => {
const currentUser: UserAttributes = (req as any).user;
const id = req.params.id;
try {
const isDeleted = await deleteProduct(req, res);
if (isDeleted) {
const loggedInUser: any = req.user;
const product = await Product.findOne({ where: { id, userId: loggedInUser.id } });
//@ts-ignore
await mailService.sendNotification(currentUser.email, "Product deleted", removedProductTemplate(currentUser.username, isDeleted?.name));
await mailService.sendNotification(currentUser.email, "Product deleted", removedProductTemplate(currentUser.username, product?.name));

const notification = await Notification.create({
title: "Product Deleted",
//@ts-ignore
message: `Your Product ${isDeleted.name} successfully deleted`,
//@ts-ignore
userId: currentUser.id || undefined,

message: `Your Product ${product?.name} successfully deleted`,
userId: currentUser.id as number,
});

notificationEmitter.emit("deleted", notification.dataValues);
Expand Down
Loading