Skip to content

Commit

Permalink
feat/add items list in discord message
Browse files Browse the repository at this point in the history
  • Loading branch information
tuturd committed Sep 21, 2024
1 parent 1755d81 commit a7a09ec
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
36 changes: 1 addition & 35 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,40 +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...');

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;
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(`Discord message sent to : ${discordId}`);
} catch (error) {
log.warn(`Discord message error : ${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 '../utils/log';

Check failure on line 3 in src/utils/sendDiscordMessage.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Useless path segments for "../utils/log", should be "./log"

Check failure on line 3 in src/utils/sendDiscordMessage.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Useless path segments for "../utils/log", should be "./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

Check failure on line 10 in src/utils/sendDiscordMessage.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unexpected string concatenation

Check failure on line 10 in src/utils/sendDiscordMessage.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Unexpected string concatenation
.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: content},

Check failure on line 34 in src/utils/sendDiscordMessage.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Expected property shorthand

Check failure on line 34 in src/utils/sendDiscordMessage.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Expected property shorthand
{ headers: { Authorization: `Bot ${token}` } }
);
log.info(`Discord message sent to : ${discordId}`);
} catch (error) {
log.warn(`Discord message error : ${error}`);
}
};

export default sendOrderToDiscordApi;

0 comments on commit a7a09ec

Please sign in to comment.