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-message #40

Merged
merged 5 commits into from
Sep 21, 2024
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
12 changes: 4 additions & 8 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
3 changes: 0 additions & 3 deletions src/controllers/order/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -122,8 +121,6 @@ const create = async (req: BodyRequest<Body>, res: Response) => {
}),
);

await sendSlackMessage();

await notifyOrdersUpdated(req.app.locals.io);

return created(res);
Expand Down
3 changes: 0 additions & 3 deletions src/controllers/order/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -70,8 +69,6 @@ const dispatch = async (req: BodyRequest<BuckResponse>, res: Response<unknown, O
}),
);

await sendSlackMessage();

await notifyOrdersUpdated(req.app.locals.io);

return created(res);
Expand Down
35 changes: 1 addition & 34 deletions src/controllers/order/editStatus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Request, Response} from 'express';
import axios from 'axios';
import notifyOrdersUpdated from '../../sockets/notifyOrdersUpdated';
import {badRequest, noContent, notFound, unauthorized} from '../../utils/responses';
import Order from '../../models/order';
Expand All @@ -8,39 +7,7 @@ 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 {
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 {
Expand Down
43 changes: 43 additions & 0 deletions src/utils/sendDiscordMessage.ts
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 0 additions & 16 deletions src/utils/sendSlackMessage.ts

This file was deleted.

Loading