forked from Karalix/micro-cafe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush-function.js
More file actions
35 lines (30 loc) · 1.25 KB
/
Copy pathpush-function.js
File metadata and controls
35 lines (30 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { Client, Databases, Messaging } from 'node-appwrite';
// This Appwrite function will be executed every time your function is triggered
export default async ({ req, res, log, error }) => {
const client = new Client()
.setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
.setKey(req.headers['x-appwrite-key']);
const databases = new Databases(client);
const messaging = new Messaging(client);
// Get the created document ID from the string databases.cafe.collections.order.documents.682f46c700164c728d6c.create
const documentId = req.headers['x-appwrite-event'].split('.')[5];
log('Triggering order: ' + documentId)
const order = await databases.getDocument('cafe', 'order', documentId);
log(order)
// parse the user id from string 'read(\"user:6817b42100095a211cf0\")'
const baristaId = order.cafeId.$permissions[0].split(':')[1].split('"')[0];
log('Barista ID: ' + baristaId)
messaging.createPush(
'unique()',
`New ${order.item.name} order received !`,
`From ${order.clientName}`,
[],
[baristaId]
)
return res.json({
motto: "Build like a team of hundreds_",
learn: "https://appwrite.io/docs",
connect: "https://appwrite.io/discord",
getInspired: "https://builtwith.appwrite.io",
});
};