Skip to content

Commit

Permalink
Fix/discord messages (#38)
Browse files Browse the repository at this point in the history
* docs: updated docs for seeding and production builds

* Revert "docs: updated docs for seeding and production builds"

This reverts commit ef2d7c6.
(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
  • Loading branch information
TeddyRoncin authored Dec 1, 2023
1 parent 6e78726 commit 57472ba
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/controllers/order/editStatus.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
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;
log.info('Sending order to discord...');
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);
Expand Down Expand Up @@ -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);

Expand Down
17 changes: 17 additions & 0 deletions src/models/placeAndDiscord.ts
Original file line number Diff line number Diff line change
@@ -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<PlaceAndDiscord> {
@AllowNull(false)
@PrimaryKey
@Column
public place: string;

@AllowNull(false)
@Column
public discordId: string;
}

0 comments on commit 57472ba

Please sign in to comment.