-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailbox-handler.js
More file actions
92 lines (72 loc) · 2.32 KB
/
mailbox-handler.js
File metadata and controls
92 lines (72 loc) · 2.32 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict'
const aws = require('aws-sdk')
const _ = require('highland')
const uuid = require('uuid')
const db = new aws.DynamoDB.DocumentClient()
const kinesis = new aws.Kinesis()
module.exports.publish = (event, context, callback) => {
_(event.Records)
.map(convertDynamoDBRecordToUoW)
.tap(uow => console.log('uow: %j', uow))
.filter(filterForInsert)
.flatMap(publishEvent)
.collect()
.toCallback(callback)
}
const convertDynamoDBRecordToUoW = (record) => {
const uow = {
event: record,
item: {
keys: aws.DynamoDB.Converter.unmarshall(record.dynamodb.Keys),
oldImage: aws.DynamoDB.Converter.unmarshall(record.dynamodb.OldImage),
newImage: aws.DynamoDB.Converter.unmarshall(record.dynamodb.NewImage)
}
}
return uow
}
const filterForInsert = (uow) => uow.event.eventName === 'INSERT'
const publishEvent = (uow) => {
const streamEvent = {
id: uuid.v1(),
type: 'mailbox-created',
timestamp: Date.now(),
item: uow.item
}
console.log('kinesis event: %j', streamEvent)
const params = {
StreamName: process.env.STREAM_NAME,
PartitionKey: uow.item.newImage.id,
Data: new Buffer.from(JSON.stringify(streamEvent)),
}
return _(kinesis.putRecord(params).promise())
}
module.exports.subscribe = (event, context, callback) => {
_(event.Records)
.map(convertKinesisRecordToUow)
.filter(filterForFileCreated)
.tap(uow => console.log('uow: %j', uow))
//.tap(itsAllGonePeteTong)
.flatMap(createMailbox)
.collect()
.toCallback(callback)
}
const convertKinesisRecordToUow = (record) => ({ event: JSON.parse(new Buffer.from(record.kinesis.data, 'base64')) })
const filterForFileCreated = (uow) => uow.event.type === 'listing-created' || uow.event.type === 'transaction-created'
const itsAllGonePeteTong = () => {
throw new Error('Something went wrong... :\'(')
}
const createMailbox = (uow) => {
const randomNumber = Math.floor(Math.random() * (9999-1000) + 1000)
const mailbox = `${uow.event.item.newImage.address.replace(/\s/g,'')}${randomNumber}@skyslope.com`
const item = {
mailbox,
id: uow.event.item.newImage.id,
address: uow.event.item.newImage.address
}
console.log('new mailbox: %j', item)
const params = {
TableName: process.env.MAILBOX_TABLE,
Item: item,
}
return _(db.put(params).promise())
}