diff --git a/.env.example b/.env.example index 2eadc23..e97d5c7 100644 --- a/.env.example +++ b/.env.example @@ -16,13 +16,9 @@ APP_TOKEN_SECRET=randomString APP_TOKEN_EXPIRES=3 days APP_PIN_TV=123456 -APP_PIN_SELLER=147258 -APP_PIN_PIZZA=741852 -APP_PIN_ADMIN=159753 -APP_PIN_PREPARATOR=000000 - -# Envoie un message sur Slack si activé à chaque commande -SLACK_ENABLED=false -SLACK_WEBHOOK_URL= +APP_PIN_SELLER=234567 +APP_PIN_ADMIN=345678 +APP_PIN_PREPARATOR=456789 +# Envoie un message sur Discord à chaque commande prête DISCORD_API_PRIVATE_KEY= diff --git a/src/controllers/order/create.ts b/src/controllers/order/create.ts index e8bd6dc..3389980 100644 --- a/src/controllers/order/create.ts +++ b/src/controllers/order/create.ts @@ -8,7 +8,6 @@ import { BodyRequest, PaymentMethod, Error, Status } from '../../types'; import Item from '../../models/item'; import errorHandler from '../../utils/errorHandler'; import Category from '../../models/category'; -import sendSlackMessage from '../../utils/sendSlackMessage'; import Supplement from '../../models/supplement'; import OrderSupplement from '../../models/orderSupplement'; import Transaction from '../../models/transaction'; @@ -122,8 +121,6 @@ const create = async (req: BodyRequest, res: Response) => { }), ); - await sendSlackMessage(); - await notifyOrdersUpdated(req.app.locals.io); return created(res); diff --git a/src/controllers/order/dispatch.ts b/src/controllers/order/dispatch.ts index 6b53e95..d15e267 100644 --- a/src/controllers/order/dispatch.ts +++ b/src/controllers/order/dispatch.ts @@ -6,7 +6,6 @@ import OrderItem from '../../models/orderItem'; import { BodyRequest, Error, Status } from '../../types'; import Item from '../../models/item'; import errorHandler from '../../utils/errorHandler'; -import sendSlackMessage from '../../utils/sendSlackMessage'; import Supplement from '../../models/supplement'; import OrderSupplement from '../../models/orderSupplement'; import Transaction from '../../models/transaction'; @@ -70,8 +69,6 @@ const dispatch = async (req: BodyRequest, res: Response { - const token = process.env.DISCORD_API_PRIVATE_KEY; - log.info('Sending order to discord...'); - log.info(order); - - try { - 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: `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); - } -}; +import sendOrderToDiscordApi from '../../utils/sendDiscordMessage'; const editStatus = (orderUpdate: OrderUpdate) => async (req: Request, res: Response) => { try { diff --git a/src/utils/sendDiscordMessage.ts b/src/utils/sendDiscordMessage.ts new file mode 100644 index 0000000..65f175f --- /dev/null +++ b/src/utils/sendDiscordMessage.ts @@ -0,0 +1,43 @@ +import axios from 'axios'; +import Order from '../models/order'; +import log from './log'; +import PlaceAndDiscord from "../models/placeAndDiscord"; + +const sendOrderToDiscordApi = async (order: Order) => { + const token = process.env.DISCORD_API_PRIVATE_KEY; + log.info('Sending order to discord...'); + + const items = `- ${ order.orderItems + .map(orderItem => orderItem.item.name) + .join('\n- ')}`; + + try { + const discordId: string | null = (await PlaceAndDiscord.findByPk(order.place))?.discordId; + if (!discordId) { + log.info('No discord id found for this place, aborting'); + return; + } + log.info(`Discord id found : ${discordId}`); + const res = await axios.post( + `https://discord.com/api/users/@me/channels`, + { recipient_id: discordId }, + { + headers: { + Authorization: `Bot ${token}`, + }, + }, + ); + const { id: dmId }: { id: string } = res.data; + const content = `Ta commande est prête, viens la chercher !\n\nDétail de la commande :\n${items}\n\nNuméro de commande : **${order.place}**`; + await axios.post( + `https://discord.com/api/channels/${dmId}/messages`, + { content }, + { headers: { Authorization: `Bot ${token}` } } + ); + log.info(`Discord message sent to : ${discordId}`); + } catch (error) { + log.warn(`Discord message error : ${error}`); + } +}; + +export default sendOrderToDiscordApi; \ No newline at end of file diff --git a/src/utils/sendSlackMessage.ts b/src/utils/sendSlackMessage.ts deleted file mode 100644 index 9472a04..0000000 --- a/src/utils/sendSlackMessage.ts +++ /dev/null @@ -1,16 +0,0 @@ -import axios from 'axios'; - -export default async () => { - if (process.env.SLACK_ENABLED === 'true') { - await axios.request({ - method: 'POST', - url: process.env.SLACK_WEBHOOK_URL, - headers: { - 'Content-Type': 'application/json', - }, - data: { - text: '<@U96RD3L93> Chef ! Une nouvelle commande !\n\nP.S.: Fuck <@UCK34T5AQ>', - }, - }); - } -};