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/discord messages #38

Merged
merged 5 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
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
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;
}