-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupsNotification.js
More file actions
64 lines (46 loc) · 2.47 KB
/
groupsNotification.js
File metadata and controls
64 lines (46 loc) · 2.47 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
import axios from 'axios'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc.js'
import timezone from 'dayjs/plugin/timezone.js'
dayjs.extend(utc)
dayjs.extend(timezone)
const timezoneBR = 'America/Sao_Paulo'
const SLACK_WEBHOOK = process.env.SLACK_WEBHOOK
const BISPER_WEBHOOK = process.env.BISPER_WEBHOOK
const groupsNamesDictionary = {
'Taxados Ingl�rios': 'Taxados Inglórios'
}
const BISPER_DEBOUNCE_INTERVAL = 4 * 60 * 60 * 1000 // 4 hours
let lastBisperNotification = dayjs().subtract(3, 'hour')
function formatGroupsMessage(ranking) {
const sortedGroups = Object.keys(ranking).sort((a, b) => ranking[a].position - ranking[b].position)
const detailedPositions = sortedGroups.reduce((msg, groupName) => {
const groupInfo = ranking[groupName]
const positionChange = groupInfo.prevRelease?.position - groupInfo.position
const positivePositionChangeText = positionChange > 0 ? `:arrow_up: ${positionChange}` : ''
const negativePositionChangeText = positionChange < 0 ? `:small_red_triangle_down: ${positionChange * -1}` : ''
const positionChangeText = positivePositionChangeText || negativePositionChangeText
const pointsChange = groupInfo.points - groupInfo.prevRelease?.points
const pointsChangeFormatted = pointsChange ? Number(pointsChange.toFixed(2)) : null
const pointsChangeText = pointsChangeFormatted ? (pointsChangeFormatted > 0 ? `(+${pointsChangeFormatted})` : `(${pointsChangeFormatted})`) : ''
return `${msg} ${groupInfo.position}º ${groupsNamesDictionary[groupName] || groupName}: ${groupInfo.points}${pointsChangeText} ${positionChangeText} \n`
}, '')
const dateLabel = dayjs().tz(timezoneBR).format('D, MMMM, YYYY')
const message = `:wave: Ranking de Grupos do IEE :statue_of_liberty: \n\n :spiral_calendar_pad: ${dateLabel} \n\n ${detailedPositions}`
return message
}
async function notifyGroups(ranking) {
const message = formatGroupsMessage(ranking)
console.log('notifyGroups', message)
const now = dayjs()
const shouldSendBisper = now.diff(lastBisperNotification, 'millisecond') >= BISPER_DEBOUNCE_INTERVAL
console.log('lastGroupsBisperNotification', lastBisperNotification.format(), now.format())
console.log('shouldSendGroupsBisper', shouldSendBisper)
const promises = [axios.post(SLACK_WEBHOOK, { text: message })]
if (shouldSendBisper) {
promises.push(axios.post(BISPER_WEBHOOK, { text: message }))
lastBisperNotification = now
}
await Promise.all(promises)
}
export { notifyGroups }