From 57472babc7374c6481c95e9044e0df5243b88d2b Mon Sep 17 00:00:00 2001 From: Teddy Roncin Date: Fri, 1 Dec 2023 14:56:18 +0100 Subject: [PATCH] Fix/discord messages (#38) * docs: updated docs for seeding and production builds * Revert "docs: updated docs for seeding and production builds" This reverts commit ef2d7c64d990b3493c849a212d71d7ac02b0eb5c. (i'm just an idiot) * feat: send messages when order is ready * fix: messages are now sent to the right person * fix: lint and a bug --- src/controllers/order/editStatus.ts | 33 ++++++++++++++++++++--------- src/models/placeAndDiscord.ts | 17 +++++++++++++++ 2 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 src/models/placeAndDiscord.ts diff --git a/src/controllers/order/editStatus.ts b/src/controllers/order/editStatus.ts index 25c3c8b..4387b97 100644 --- a/src/controllers/order/editStatus.ts +++ b/src/controllers/order/editStatus.ts @@ -1,14 +1,15 @@ -import { Request, Response } from 'express'; +import {Request, Response} from 'express'; import axios from 'axios'; import notifyOrdersUpdated from '../../sockets/notifyOrdersUpdated'; -import { badRequest, noContent, notFound, unauthorized } from '../../utils/responses'; +import {badRequest, noContent, notFound, unauthorized} from '../../utils/responses'; import Order from '../../models/order'; import errorHandler from '../../utils/errorHandler'; -import { Error, OrderUpdate, Permission, Status } from '../../types'; +import {Error, OrderUpdate, Permission, Status} from '../../types'; import OrderItem from '../../models/orderItem'; import Item from '../../models/item'; import Category from '../../models/category'; import log from '../../utils/log'; +import PlaceAndDiscord from "../../models/placeAndDiscord"; const sendOrderToDiscordApi = async (order: Order) => { const token = process.env.DISCORD_API_PRIVATE_KEY; @@ -16,17 +17,25 @@ const sendOrderToDiscordApi = async (order: Order) => { log.info(order); try { - await axios.post( - `https://bouffe-discord.ua.uttnetgroup.fr/sendMessageToDiscord`, - { - order, - }, + const discordId: string | null = (await PlaceAndDiscord.findByPk(order.place))?.discordId; + if (!discordId) { + return; + } + const res = await axios.post( + `https://discord.com/api/users/@me/channels`, + { recipient_id: discordId }, { headers: { - Authorization: `Bearer ${token}`, + Authorization: `Bot ${token}`, }, }, ); + const { id: dmId }: { id: string } = res.data; + await axios.post( + `https://discord.com/api/channels/${dmId}/messages`, + { content: "Ta commande est prĂȘte, viens la chercher !" }, + { headers: { Authorization: `Bot ${token}` } } + ); log.info('SENT !'); } catch (error) { log.warn('Error while sending message to bouffe-discord', error); @@ -86,7 +95,11 @@ const editStatus = (orderUpdate: OrderUpdate) => async (req: Request, res: Respo await order.save(); - await sendOrderToDiscordApi(order); + if (order.status === Status.READY + && orderUpdate === OrderUpdate.UPGRADE + && order.orderItems.some((item) => item.item.category.needsPreparation)) { + await sendOrderToDiscordApi(order); + } await notifyOrdersUpdated(req.app.locals.io); diff --git a/src/models/placeAndDiscord.ts b/src/models/placeAndDiscord.ts new file mode 100644 index 0000000..a72ec1f --- /dev/null +++ b/src/models/placeAndDiscord.ts @@ -0,0 +1,17 @@ +import {Table, Column, Model, AllowNull, PrimaryKey} from 'sequelize-typescript'; + +@Table({ + tableName: 'placediscord', + paranoid: true, + timestamps: false, +}) +export default class PlaceAndDiscord extends Model { + @AllowNull(false) + @PrimaryKey + @Column + public place: string; + + @AllowNull(false) + @Column + public discordId: string; +}