playov2-js-utilities 0.3.7 → 0.3.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/message_publisher/index.js +52 -8
- package/lib/notification-templates.js +8 -4
- package/lib/request.js +6 -2
- package/lib/util.js +19 -3
- package/package.json +4 -2
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
/*
|
|
2
|
+
Read this and associated queue configs before deploying a new queue
|
|
3
|
+
https://cloud.google.com/tasks/docs/reference/rest/v2beta3/projects.locations.queues.tasks#resource:-task
|
|
4
|
+
*/
|
|
2
5
|
|
|
3
6
|
const { CloudTasksClient } = require('@google-cloud/tasks');
|
|
7
|
+
const { v4: uuidv4 } = require('uuid');
|
|
4
8
|
|
|
5
9
|
const PLAYO_LOGGER = require("@playo/logger");
|
|
6
10
|
const LOGGER = new PLAYO_LOGGER("playo-message-publisher");
|
|
7
11
|
|
|
8
|
-
// Instantiates a client.
|
|
9
12
|
const client = new CloudTasksClient();
|
|
10
13
|
|
|
11
14
|
const PROJECT = process.env.GCP_PROJECT;
|
|
12
15
|
const LOCATION = process.env.GCP_LOCATION;
|
|
13
16
|
|
|
17
|
+
const MAX_HTTP_TASK_TTL = 1800000 // milliseconds - half an hour
|
|
18
|
+
|
|
14
19
|
if (!PROJECT || !LOCATION) {
|
|
15
20
|
console.warn('You need to pass - GCP_PROJECT, GCP_LOCATION in env for message queue to work!');
|
|
16
21
|
}
|
|
@@ -28,9 +33,11 @@ if (!PROJECT || !LOCATION) {
|
|
|
28
33
|
* @param {Object} messageProcessingProperties - {
|
|
29
34
|
* delay
|
|
30
35
|
* }
|
|
36
|
+
* @param {String} taskId -used as de-duplication key
|
|
37
|
+
* @param {Date} ttl - utc date format
|
|
31
38
|
* @param {String} requestId
|
|
32
39
|
*/
|
|
33
|
-
async function createHttpTask(queueId, payload, messageProcessingProperties, requestId) {
|
|
40
|
+
async function createHttpTask(queueId, payload, messageProcessingProperties, taskId, ttl, requestId) {
|
|
34
41
|
|
|
35
42
|
try {
|
|
36
43
|
const project = PROJECT;
|
|
@@ -41,7 +48,14 @@ async function createHttpTask(queueId, payload, messageProcessingProperties, req
|
|
|
41
48
|
|
|
42
49
|
const parent = client.queuePath(project, location, queue);
|
|
43
50
|
|
|
44
|
-
const task = {
|
|
51
|
+
const task = {
|
|
52
|
+
httpRequest: payload,
|
|
53
|
+
name: ''
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
task.name = getTaskId(project, location, queue, taskId);
|
|
57
|
+
|
|
58
|
+
task.dispatchDeadline = _getHTTPTaskTTL(ttl); // nano seconds
|
|
45
59
|
|
|
46
60
|
if (payload.body) {
|
|
47
61
|
task.httpRequest.body = Buffer.from(payload.body).toString('base64');
|
|
@@ -52,19 +66,49 @@ async function createHttpTask(queueId, payload, messageProcessingProperties, req
|
|
|
52
66
|
task.scheduleTime = {
|
|
53
67
|
seconds: delay + Date.now() / 1000,
|
|
54
68
|
};
|
|
55
|
-
}
|
|
69
|
+
};
|
|
56
70
|
|
|
57
|
-
// Send create task request.
|
|
58
71
|
LOGGER.info(requestId, task, `Sending task to queue ${queueId}}`)
|
|
59
72
|
|
|
60
73
|
const request = { parent: parent, task: task };
|
|
61
74
|
const [response] = await client.createTask(request);
|
|
62
|
-
|
|
75
|
+
|
|
76
|
+
LOGGER.info(requestId, { name: response.name, response }, `Added task to queue ${queueId}}`)
|
|
63
77
|
} catch (err) {
|
|
64
78
|
LOGGER.error(requestId, err, err.message)
|
|
65
79
|
}
|
|
66
80
|
};
|
|
67
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Returns a unique string for name of a task in task queues, to be used for task de-duplication purpose.
|
|
84
|
+
* @param {String} projectId
|
|
85
|
+
* @param {String} locationId
|
|
86
|
+
* @param {String} queueId
|
|
87
|
+
* @param {String} taskId
|
|
88
|
+
* @returns {String}
|
|
89
|
+
*/
|
|
90
|
+
const getTaskId = (projectId, locationId, queueId, taskId = uuidv4()) => {
|
|
91
|
+
return `projects/${projectId}/locations/${locationId}/queues/${queueId}/tasks/${taskId}`;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Returns ttl for http tasks in nanoseconds
|
|
96
|
+
* https://cloud.google.com/tasks/docs/reference/rest/v2beta3/projects.locations.queues.tasks#resource:-task
|
|
97
|
+
* @param {Date} ttl
|
|
98
|
+
*/
|
|
99
|
+
const _getHTTPTaskTTL = (ttl) => {
|
|
100
|
+
|
|
101
|
+
const currentTimestamp = new Date().getTime()
|
|
102
|
+
const providedTTLTimestamp = new Date(ttl).getTime();
|
|
103
|
+
|
|
104
|
+
if (providedTTL > currentTimestamp) {
|
|
105
|
+
return Math.min(providedTTLTimestamp, MAX_HTTP_TASK_TTL) * 1000000
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return MAX_HTTP_TASK_TTL * 1000000;
|
|
109
|
+
}
|
|
110
|
+
|
|
68
111
|
module.exports = {
|
|
69
|
-
createHttpTask
|
|
112
|
+
createHttpTask,
|
|
113
|
+
getTaskId
|
|
70
114
|
};
|
|
@@ -205,13 +205,17 @@ const host_cancelled_activity = {
|
|
|
205
205
|
};
|
|
206
206
|
|
|
207
207
|
/**
|
|
208
|
-
*
|
|
208
|
+
* Template for sending activity reminder.
|
|
209
|
+
* Notification is received by all the players of the activity.
|
|
210
|
+
* Text render only
|
|
211
|
+
* @param {String} sport_name
|
|
212
|
+
* @param {String} date
|
|
209
213
|
*/
|
|
210
214
|
const activity_reminder = {
|
|
211
|
-
heading: '
|
|
212
|
-
text: 'You have a {sport_name}} game coming up
|
|
215
|
+
heading: 'Activity Reminder',
|
|
216
|
+
text: 'You have a {{sport_name}} game coming up at {{date}}. Gear up for some fun!',
|
|
213
217
|
buttons: [],
|
|
214
|
-
notificationId: notificationIds.ACTIVITY_REMINDER
|
|
218
|
+
notificationId: notificationIds.ACTIVITY_REMINDER
|
|
215
219
|
};
|
|
216
220
|
|
|
217
221
|
const host_updated_activity_details = {
|
package/lib/request.js
CHANGED
|
@@ -17,10 +17,14 @@ const playoResponseHandler = (resp, requestId) => {
|
|
|
17
17
|
if (status === 200) {
|
|
18
18
|
const { requestStatus } = data;
|
|
19
19
|
LOGGER.info(requestId, data, `Api call to url to ${url} succeeded with status ${status}`);
|
|
20
|
-
|
|
20
|
+
if (!requestStatus) {
|
|
21
|
+
data['requestStatus'] = 1; // assume request status of 1 if any service doesn't provide requestStatus.
|
|
22
|
+
}
|
|
23
|
+
return data;
|
|
21
24
|
} else {
|
|
22
25
|
LOGGER.error(requestId, data, `Api call to url to ${url} failed with status : ${status}`);
|
|
23
|
-
|
|
26
|
+
data['requestStatus'] = 0;
|
|
27
|
+
return Promise.reject(data);
|
|
24
28
|
}
|
|
25
29
|
};
|
|
26
30
|
|
package/lib/util.js
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
* Ideally new repositories should import utils from here only. Todo- look for a workaround - named imports
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
const hbs = require('handlebars');
|
|
8
|
+
|
|
7
9
|
/**
|
|
8
10
|
* Finds rating given to a user for a sport by playpals <Assumes playpalDocs supplied are already filtered for a user>
|
|
9
11
|
* @param {Array} playpalDocs
|
|
@@ -125,12 +127,26 @@ const getCategoricalBreakUpFromRecentUserRatings = (palRatingDict, levelCategori
|
|
|
125
127
|
|
|
126
128
|
const noop = () => {
|
|
127
129
|
return undefined;
|
|
128
|
-
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
*
|
|
134
|
+
* @param {String} template - Handlebars compatible template
|
|
135
|
+
* @param {Object} data - Data points containing key value pairs of data required in template supplied
|
|
136
|
+
* @returns {String}
|
|
137
|
+
*/
|
|
138
|
+
const renderTemplate = (template, data) => {
|
|
139
|
+
|
|
140
|
+
const temp = hbs.compile(template);
|
|
141
|
+
|
|
142
|
+
return temp(data);
|
|
143
|
+
};
|
|
129
144
|
|
|
130
145
|
module.exports = {
|
|
131
146
|
getRatingsFromPlaypalDocs,
|
|
132
147
|
findAverage,
|
|
133
148
|
getRecentRatingDictOfPals,
|
|
134
149
|
getCategoricalBreakUpFromRecentUserRatings,
|
|
135
|
-
noop
|
|
136
|
-
|
|
150
|
+
noop,
|
|
151
|
+
renderTemplate
|
|
152
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "playov2-js-utilities",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.12",
|
|
4
4
|
"description": "Private package for JS utility functions",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -24,6 +24,8 @@
|
|
|
24
24
|
"@google-cloud/tasks": "^2.5.0",
|
|
25
25
|
"@playo/logger": "^0.9.18",
|
|
26
26
|
"axios": "^0.24.0",
|
|
27
|
-
"cron": "^1.8.2"
|
|
27
|
+
"cron": "^1.8.2",
|
|
28
|
+
"handlebars": "^4.7.7",
|
|
29
|
+
"uuid": "^8.3.2"
|
|
28
30
|
}
|
|
29
31
|
}
|