-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
127 lines (121 loc) · 4.12 KB
/
db.js
File metadata and controls
127 lines (121 loc) · 4.12 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const Knex = require('knex');
const { PubSub } = require('@google-cloud/pubsub');
const pubSubClient = new PubSub();
const topicName = 'notification';
const connectionObject = {
'development': {
client: 'pg',
connection: {
host: process.env.DB_DEV_URL,
user: process.env.DB_DEV_USERNAME,
password: process.env.DB_DEV_PASSWORD,
database: process.env.DB_DEV_NAME
},
// debug: true
},
'production': {
client: 'pg',
connection: {
host: process.env.DB_PROD_URL,
user: process.env.DB_PROD_USERNAME,
password: process.env.DB_PROD_PASSWORD,
database: process.env.DB_PROD_NAME
}
}
}
const getConnection = () => {
const env = process.env.NODE_ENV || 'development';
return connectionObject[env];
}
const upsert = async (params) => {
const { table, object, constraint } = params;
const insert = knex(table).insert(object);
const update = knex.queryBuilder().update(object);
const query = await knex.raw(`? ON CONFLICT ${constraint} DO ? returning *`, [insert, update]).get('rows').get(0);
return query;
};
let knex;
exports.save = async shows => {
try {
await connect();
shows.map(async show => {
const showEntry = await knex.select().table('shows').where('timestamp', show.unixTime).limit(1);
console.log(showEntry);
if (showEntry.length) {
const showHistory = await knex.select().table('show_histories').where('show_id', showEntry[0].id).orderBy('id', 'desc').limit(1);
if (JSON.stringify(JSON.parse(showHistory[0].new_members)) !== JSON.stringify(show.showMembers)) {
const compareResult = compare(JSON.parse(showHistory.length ? showHistory.new_members ? showHistory.new_members : "[]" : "[]"), show.showMembers);
const showHistoryObj = {
show_id: showEntry[0].id,
old_members: showHistory[0].new_members,
new_members: JSON.stringify(compareResult.newEntry)
}
const showDetails = {
'title': show.title,
'show_id': showEntry[0].id
}
await knex('show_histories').insert(showHistoryObj);
await notifyUpdate('member', 'delete', compareResult.replacedEntry, showDetails);
await notifyUpdate('member', 'insert', compareResult.newEntry, showDetails);
}
} else {
let teamName;
show.team === 'Academy A' ? teamName = 'Academy' : teamName = show.team;
const setlist = await knex.select('id').table('setlists').where('name', show.title);
const team = await knex.select('id').table('teams').where('name', teamName);
const result = await knex('shows').insert({
setlist_id: setlist[0].id,
date: show.showDate,
timestamp: show.unixTime,
team_id: team[0].id,
vip: show.order.VIP,
ofc: show.order.OFC,
gen: show.order.GENERAL,
is_event: show.isEvent,
event_name: show.eventName,
event_member: show.eventMember,
ticket_closed: show.order.GENERAL.end
}).returning('*');
const showHistory = {
show_id: result[0].id,
old_members: JSON.stringify([]),
new_members: JSON.stringify(show.showMembers || [])
};
const showDetails = {
'title': show.title,
'show_id': result[0].id
}
await knex('show_histories').insert(showHistory);
await notifyUpdate('setlist', '', [show.showName], showDetails);
}
})
return;
} catch (e) {
console.error(e);
}
}
const compare = (oldData, newData) => {
const oldValue = oldData || [];
const newValue = newData;
let replacedEntry = oldValue.filter(x => !newValue.includes(x));
let newEntry = (newValue.filter(x => !oldValue.includes(x)));
return {
newEntry,
replacedEntry
};
}
const notifyUpdate = async (type, action, data, showDetails) => {
try {
return await pubSubClient.topic(topicName).publish(Buffer.from(JSON.stringify({
type,
action,
data,
showDetails
})));
} catch (e) {
console.error(e);
}
}
const connect = () => {
return knex ? knex : knex = Knex(getConnection());
}