Skip to content

Commit

Permalink
Changed mails
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoFS96 committed Mar 16, 2022
1 parent 4ebd9ce commit 9db53b7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/controllers/order.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ export async function validateOrder(userId: string, shopId: string) {
}
}


// ================ ================ ================ ================ ================

export const createNewOrden = async (
userId: string,
shopId: string,
products: any, // array de productos
total: any,
total: any
) => {
try {
const orden = await prisma.orders.create({
Expand Down Expand Up @@ -259,15 +258,15 @@ export async function updateProductsStocks(id: string) {
}
}

//Enviar Email cuando state se cambia a Enviando
async function notify(id: string, message: string) {
//Enviar Email cuando state se cambia a Enviando / Creado
export async function notify(id: string, message: string) {
const userId: any = await prisma.orders.findUnique({
where: {
id,
},
select: {
userId: true,
},
// select: {
// userId: true,
// },
});
const email: any = await prisma.users.findMany({
where: {
Expand All @@ -277,6 +276,7 @@ async function notify(id: string, message: string) {
email: true,
},
});
message = message + `\n Detalles de su Orden: \n ${userId.data}`;
sendEmail(email[0].email, message);
}

Expand All @@ -287,7 +287,7 @@ export async function orderProducts(id: string) {
id,
},
});
console.log(orderProducts)
console.log(orderProducts);
return orderProducts;
} catch (error) {
return null;
Expand Down
33 changes: 33 additions & 0 deletions src/controllers/product.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Products } from "@prisma/client";
import prisma from "../database/db";
import { Product, Producto, ProductOptions } from "../Items/Product.interface";
import { sendEmail } from "./email.controller";

async function namesCategories(product: any) {
let arrCategories: any[] = await prisma.categories.findMany({
Expand Down Expand Up @@ -199,9 +200,11 @@ export const filterByDiscount = async (discount: number, shopId: string) => {
export const saveNewProduct = async (data: any) => {
// TODO: especificar los datos que se deben de recibir de forma obligatoria
try {
notifyMailingList(data.shopId);
const newProduct: any = await prisma.products.create({
data: data,
});
console.log(newProduct);
for (let i = 0; i < data.categoriesId.length; i++) {
let idPro = await prisma.categories.findUnique({
where: {
Expand Down Expand Up @@ -379,3 +382,33 @@ export const getProductsNames = async (shopId: string) => {
return null;
}
};

const notifyMailingList = async (shopId: string) => {
try {
const users = await prisma.users.findMany({
where: {
mailingList: true,
},
});
const usersFavourited = users.filter((e) =>
e.favouriteShops.includes(shopId)
);
const shop = await prisma.shops.findUnique({
where: {
id: shopId,
},
select: {
name: true,
},
});
const emails = usersFavourited.map((e) => e.email);
for (let i = 0; i < emails.length; i++) {
sendEmail(
emails[i],
` Tu tienda favorita ${shop?.name} ha publicado un nuevo producto!!! \n Ven antes de que se lo lleven! \n https://humblefood.vercel.app/productShop/${shopId}`
);
}
} catch (error) {
return null;
}
};
7 changes: 4 additions & 3 deletions src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,17 @@ export const removeFavourite = async (userId: string, shopId: string) => {

export const updateUserMailingState = async (userId: string, boolean: any) => {
try {
let bool = boolean as boolean;
const updatedUser = await prisma.users.update({
let bool: boolean;
if (boolean === "true") bool = true;
else bool = false;
const updatedUser: any = await prisma.users.update({
where: {
id: userId,
},
data: {
mailingList: bool,
},
});

return updatedUser;
} catch (error) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ router.get("/orders/:id", getAllOrders); //id de shops o users o orders
router.get("/orderProducts/:id", getOrderProducts);
router.put("/order", updateOrderProducts); //recibe id, products por body
router.put("/order/:id/:state", updateOrder); // recibe id de la orden
router.post("/order", createOrder);
router.post("/order", createOrder); // --> necesita: shopId, total int, userId, products [id, cantidad int]
// router.post("/order", saveOrder);

// router.get("/carrito/:idUser", getCarrito);
Expand Down

0 comments on commit 9db53b7

Please sign in to comment.