-
Notifications
You must be signed in to change notification settings - Fork 8
/
product.event.service.js
44 lines (35 loc) · 1.05 KB
/
product.event.service.js
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
36
37
38
39
40
41
42
43
44
const AWS = require("aws-sdk");
const { productFromJson } = require("./product");
const { createEvent } = require("./product.event")
const TOPIC_ARN =
process.env.TOPIC_ARN || "arn:aws:sns:ap-southeast-2:000000000000:products";
class ProductEventService {
async create(event) {
const product = productFromJson(event);
await this.publish(createEvent(product, "CREATED"));
}
async update(event) {
const product = productFromJson(event);
await this.publish(createEvent(product, "UPDATED"));
}
async delete(event) {
const product = productFromJson(event);
await this.publish(createEvent(product, "DELETED"));
}
async publish(message) {
const SNS = new AWS.SNS({
endpoint: process.env.AWS_SNS_ENDPOINT,
region: process.env.AWS_REGION
});
const params = {
Message: JSON.stringify(message),
TopicArn: TOPIC_ARN,
};
console.log("ProductEventService - sending message:", message);
return SNS.publish(params).promise();
}
}
module.exports = {
ProductEventService,
createEvent,
};