playov2-js-utilities 0.3.70 → 0.3.73
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/README.md +28 -28
- package/index.js +4 -4
- package/lib/constants.js +20 -20
- package/lib/cron.js +43 -43
- package/lib/db/models/user.js +112 -0
- package/lib/db/mongo.js +3 -0
- package/lib/db_interface/mongo/index.js +5 -5
- package/lib/db_interface/mongo/schemas/activitySchemas/_activityIndex.js +10 -10
- package/lib/db_interface/mongo/schemas/activitySchemas/activityInsurance.js +17 -17
- package/lib/db_interface/mongo/schemas/activitySchemas/activityLocation.js +104 -104
- package/lib/db_interface/mongo/schemas/activitySchemas/activityQueryBlock.js +11 -11
- package/lib/db_interface/mongo/schemas/activitySchemas/activityView.js +13 -13
- package/lib/db_interface/mongo/schemas/activitySchemas/cronJobLog.js +12 -12
- package/lib/db_interface/mongo/schemas/activitySchemas/playogame.request.js +37 -37
- package/lib/db_interface/mongo/schemas/activitySchemas/scheduled.tasks.js +52 -52
- package/lib/db_interface/mongo/schemas/activitySchemas/userActivity.js +133 -133
- package/lib/db_interface/mongo/schemas/gamebookSchemas/_gamebookIndex.js +2 -2
- package/lib/db_interface/mongo/schemas/gamebookSchemas/activityNotes.js +19 -19
- package/lib/db_interface/mongo/schemas/gamebookSchemas/expense.js +36 -36
- package/lib/db_interface/mongo/schemas/gamebookSchemas/expenseLogs.js +13 -13
- package/lib/db_interface/mongo/schemas/gamebookSchemas/message.requests.js +29 -29
- package/lib/db_interface/mongo/schemas/gamebookSchemas/nonPlayoExpenseUsers.js +14 -14
- package/lib/db_interface/mongo/schemas/gamebookSchemas/playpals.js +69 -69
- package/lib/db_interface/mongo/schemas/gamebookSchemas/postActivity.js +51 -51
- package/lib/db_interface/mongo/schemas/gamebookSchemas/tags.js +12 -12
- package/lib/db_interface/mongo/schemas/userSchemas/QuickActions.js +25 -25
- package/lib/db_interface/mongo/schemas/userSchemas/_userIndex.js +7 -7
- package/lib/db_interface/mongo/schemas/userSchemas/blockedUser.js +11 -11
- package/lib/db_interface/mongo/schemas/userSchemas/city.js +62 -62
- package/lib/db_interface/mongo/schemas/userSchemas/country.js +63 -63
- package/lib/db_interface/mongo/schemas/userSchemas/emailOtp.js +12 -12
- package/lib/db_interface/mongo/schemas/userSchemas/emailVerificationToken.js +11 -11
- package/lib/db_interface/mongo/schemas/userSchemas/gt.trial.games.js +24 -24
- package/lib/db_interface/mongo/schemas/userSchemas/invite.js +14 -14
- package/lib/db_interface/mongo/schemas/userSchemas/karmaConfig.js +11 -11
- package/lib/db_interface/mongo/schemas/userSchemas/karmaLog.js +28 -28
- package/lib/db_interface/mongo/schemas/userSchemas/passwordResetToken.js +11 -11
- package/lib/db_interface/mongo/schemas/userSchemas/playo.subscription.pack.js +67 -67
- package/lib/db_interface/mongo/schemas/userSchemas/rankings.js +16 -16
- package/lib/db_interface/mongo/schemas/userSchemas/sports.js +26 -26
- package/lib/db_interface/mongo/schemas/userSchemas/sportsCategory.js +11 -11
- package/lib/db_interface/mongo/schemas/userSchemas/tempUser.js +35 -35
- package/lib/db_interface/mongo/schemas/userSchemas/trendingSports.js +14 -14
- package/lib/db_interface/mongo/schemas/userSchemas/user.activity.health.kit.data.js +26 -26
- package/lib/db_interface/mongo/schemas/userSchemas/user.contacts.js +33 -33
- package/lib/db_interface/mongo/schemas/userSchemas/user.js +111 -111
- package/lib/db_interface/mongo/schemas/userSchemas/userAlerts.js +23 -23
- package/lib/db_interface/mongo/schemas/userSchemas/userCredentials.js +50 -50
- package/lib/db_interface/mongo/schemas/userSchemas/userFavourites.js +81 -81
- package/lib/db_interface/mongo/schemas/userSchemas/userOtp.js +9 -9
- package/lib/db_interface/mongo/schemas/userSchemas/userReputation.js +21 -21
- package/lib/db_interface/mongo/schemas/userSchemas/userSocial.js +17 -17
- package/lib/db_interface/mongo/schemas/userSchemas/year.in.playo.js +51 -51
- package/lib/db_interface/mongo/schemas/venueSchemas/_venueIndex.js +3 -3
- package/lib/db_interface/mongo/schemas/venueSchemas/ameneties.js +9 -9
- package/lib/db_interface/mongo/schemas/venueSchemas/cityArea.js +45 -45
- package/lib/db_interface/mongo/schemas/venueSchemas/clubConstraints.js +17 -17
- package/lib/db_interface/mongo/schemas/venueSchemas/corporateOffer.js +17 -17
- package/lib/db_interface/mongo/schemas/venueSchemas/couponOffers.js +39 -39
- package/lib/db_interface/mongo/schemas/venueSchemas/gamebagMember.js +11 -11
- package/lib/db_interface/mongo/schemas/venueSchemas/offer.js +48 -48
- package/lib/db_interface/mongo/schemas/venueSchemas/thirdParty.js +12 -12
- package/lib/db_interface/mongo/schemas/venueSchemas/venue.js +134 -134
- package/lib/db_interface/mongo/schemas/venueSchemas/venueEnquires.js +22 -22
- package/lib/db_interface/mongo/schemas/venueSchemas/venueMembers.js +14 -14
- package/lib/db_interface/mongo/schemas/venueSchemas/venueRating.js +18 -18
- package/lib/index.js +22 -23
- package/lib/logger.js +115 -115
- package/lib/message_publisher/index.js +144 -144
- package/lib/middleware.js +46 -46
- package/lib/notification-templates.js +254 -254
- package/lib/notification.config.js +476 -476
- package/lib/playo.utils/playo.error.handler.js +23 -23
- package/lib/playo.utils/playo.http.handler.js +162 -162
- package/lib/playo.utils/playo.res.generator.js +58 -58
- package/lib/profanityFilter/profanityFilter.js +15 -16
- package/lib/profanityFilter/profanityWords.js +2 -2
- package/lib/ratings/index.js +95 -95
- package/lib/request.js +135 -135
- package/lib/responseHandler/responseHandler.js +52 -52
- package/lib/util.js +151 -151
- package/package.json +36 -36
package/lib/logger.js
CHANGED
|
@@ -1,115 +1,115 @@
|
|
|
1
|
-
const PLAYO_LOGGER = require("@playo/logger")
|
|
2
|
-
|
|
3
|
-
const LOGGER = new PLAYO_LOGGER("playov2-js-utilities")
|
|
4
|
-
|
|
5
|
-
const ENV = process.env.NODE_ENV || 'staging';
|
|
6
|
-
|
|
7
|
-
if (ENV === 'staging' || ENV === 'test') {
|
|
8
|
-
LOGGER.setSlack('# test');
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
if (ENV === "production") {
|
|
12
|
-
LOGGER.setMailer({
|
|
13
|
-
'receiverEmail': ["saurabh@playo.co", "avish@playo.co"], // array of recievers, mandatory
|
|
14
|
-
'senderEmail': "logger@playo.co",// address of sender, mandatory
|
|
15
|
-
'senderName': 'Mail Logger - prod - Utitlies module', // default: Mail Logger, optional
|
|
16
|
-
'replyEmail': 'no-reply@mail.com', // default: no-reply@playo.co, optional
|
|
17
|
-
'region': "us-east-1", // default: 'us-east-1', optional
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
LOGGER.setSlack('# server_alerts');
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Info log - use where some stack trace needs to be stored for later debug purpose
|
|
25
|
-
* @param {String} id
|
|
26
|
-
* @param {Object} data
|
|
27
|
-
* @param {String} message
|
|
28
|
-
*/
|
|
29
|
-
const prepareInfoLog = (
|
|
30
|
-
id = "info-log",
|
|
31
|
-
data = {},
|
|
32
|
-
message = "info-message"
|
|
33
|
-
) => {
|
|
34
|
-
LOGGER.info(id, data, message);
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Error log - used to monitor error throughout the system
|
|
39
|
-
* @param {String} id
|
|
40
|
-
* @param {Object} data
|
|
41
|
-
* @param {String} message
|
|
42
|
-
*/
|
|
43
|
-
const prepareErrorLog = (
|
|
44
|
-
id = "error-log",
|
|
45
|
-
data = {},
|
|
46
|
-
message = "error-message"
|
|
47
|
-
) => {
|
|
48
|
-
if (data.stack) {
|
|
49
|
-
data = data.stack;
|
|
50
|
-
}
|
|
51
|
-
LOGGER.error(id, data, message);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Alert log - Sends alert on slack and email to notify to concerned people
|
|
56
|
-
* @param {String} id
|
|
57
|
-
* @param {Object} data
|
|
58
|
-
* @param {String} message
|
|
59
|
-
* @param {Boolean} sendEmail
|
|
60
|
-
*/
|
|
61
|
-
const prepareAlertLog = (
|
|
62
|
-
id = "alert-log",
|
|
63
|
-
data = {},
|
|
64
|
-
message = "alert-message",
|
|
65
|
-
sendEmail = false
|
|
66
|
-
) => {
|
|
67
|
-
LOGGER.alert(id, data, message, sendEmail);
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Use this to debug , ideally only on staging env
|
|
72
|
-
* @param {String} id
|
|
73
|
-
* @param {Object} data
|
|
74
|
-
* @param {String} message
|
|
75
|
-
*/
|
|
76
|
-
const prepareDebugLog = (
|
|
77
|
-
id = "alert log",
|
|
78
|
-
data = {},
|
|
79
|
-
message = "debug-log"
|
|
80
|
-
) => {
|
|
81
|
-
LOGGER.debug(id, data, message);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Use this to print error or failure that do not need to fixed on an urgent basis but need to later analyzed later
|
|
86
|
-
* @param {String} id
|
|
87
|
-
* @param {Object} data
|
|
88
|
-
* @param {String} message
|
|
89
|
-
*/
|
|
90
|
-
const prepareWarnLog = (
|
|
91
|
-
id = "warn log",
|
|
92
|
-
data = {},
|
|
93
|
-
message = "warn-log",
|
|
94
|
-
topic = 'warn-topic'
|
|
95
|
-
) => {
|
|
96
|
-
LOGGER.warn(id, data, message, topic);
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Set module name for your module to be observed in logs
|
|
101
|
-
* @param {String} moduleName
|
|
102
|
-
* @returns
|
|
103
|
-
*/
|
|
104
|
-
const setModule = (moduleName) => {
|
|
105
|
-
return LOGGER.setModule(moduleName);
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
module.exports = {
|
|
109
|
-
prepareInfoLog,
|
|
110
|
-
prepareErrorLog,
|
|
111
|
-
prepareAlertLog,
|
|
112
|
-
prepareDebugLog,
|
|
113
|
-
prepareWarnLog,
|
|
114
|
-
setModule,
|
|
115
|
-
};
|
|
1
|
+
const PLAYO_LOGGER = require("@playo/logger")
|
|
2
|
+
|
|
3
|
+
const LOGGER = new PLAYO_LOGGER("playov2-js-utilities")
|
|
4
|
+
|
|
5
|
+
const ENV = process.env.NODE_ENV || 'staging';
|
|
6
|
+
|
|
7
|
+
if (ENV === 'staging' || ENV === 'test') {
|
|
8
|
+
LOGGER.setSlack('# test');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (ENV === "production") {
|
|
12
|
+
LOGGER.setMailer({
|
|
13
|
+
'receiverEmail': ["saurabh@playo.co", "avish@playo.co"], // array of recievers, mandatory
|
|
14
|
+
'senderEmail': "logger@playo.co",// address of sender, mandatory
|
|
15
|
+
'senderName': 'Mail Logger - prod - Utitlies module', // default: Mail Logger, optional
|
|
16
|
+
'replyEmail': 'no-reply@mail.com', // default: no-reply@playo.co, optional
|
|
17
|
+
'region': "us-east-1", // default: 'us-east-1', optional
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
LOGGER.setSlack('# server_alerts');
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Info log - use where some stack trace needs to be stored for later debug purpose
|
|
25
|
+
* @param {String} id
|
|
26
|
+
* @param {Object} data
|
|
27
|
+
* @param {String} message
|
|
28
|
+
*/
|
|
29
|
+
const prepareInfoLog = (
|
|
30
|
+
id = "info-log",
|
|
31
|
+
data = {},
|
|
32
|
+
message = "info-message"
|
|
33
|
+
) => {
|
|
34
|
+
LOGGER.info(id, data, message);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Error log - used to monitor error throughout the system
|
|
39
|
+
* @param {String} id
|
|
40
|
+
* @param {Object} data
|
|
41
|
+
* @param {String} message
|
|
42
|
+
*/
|
|
43
|
+
const prepareErrorLog = (
|
|
44
|
+
id = "error-log",
|
|
45
|
+
data = {},
|
|
46
|
+
message = "error-message"
|
|
47
|
+
) => {
|
|
48
|
+
if (data.stack) {
|
|
49
|
+
data = data.stack;
|
|
50
|
+
}
|
|
51
|
+
LOGGER.error(id, data, message);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Alert log - Sends alert on slack and email to notify to concerned people
|
|
56
|
+
* @param {String} id
|
|
57
|
+
* @param {Object} data
|
|
58
|
+
* @param {String} message
|
|
59
|
+
* @param {Boolean} sendEmail
|
|
60
|
+
*/
|
|
61
|
+
const prepareAlertLog = (
|
|
62
|
+
id = "alert-log",
|
|
63
|
+
data = {},
|
|
64
|
+
message = "alert-message",
|
|
65
|
+
sendEmail = false
|
|
66
|
+
) => {
|
|
67
|
+
LOGGER.alert(id, data, message, sendEmail);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Use this to debug , ideally only on staging env
|
|
72
|
+
* @param {String} id
|
|
73
|
+
* @param {Object} data
|
|
74
|
+
* @param {String} message
|
|
75
|
+
*/
|
|
76
|
+
const prepareDebugLog = (
|
|
77
|
+
id = "alert log",
|
|
78
|
+
data = {},
|
|
79
|
+
message = "debug-log"
|
|
80
|
+
) => {
|
|
81
|
+
LOGGER.debug(id, data, message);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Use this to print error or failure that do not need to fixed on an urgent basis but need to later analyzed later
|
|
86
|
+
* @param {String} id
|
|
87
|
+
* @param {Object} data
|
|
88
|
+
* @param {String} message
|
|
89
|
+
*/
|
|
90
|
+
const prepareWarnLog = (
|
|
91
|
+
id = "warn log",
|
|
92
|
+
data = {},
|
|
93
|
+
message = "warn-log",
|
|
94
|
+
topic = 'warn-topic'
|
|
95
|
+
) => {
|
|
96
|
+
LOGGER.warn(id, data, message, topic);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Set module name for your module to be observed in logs
|
|
101
|
+
* @param {String} moduleName
|
|
102
|
+
* @returns
|
|
103
|
+
*/
|
|
104
|
+
const setModule = (moduleName) => {
|
|
105
|
+
return LOGGER.setModule(moduleName);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
module.exports = {
|
|
109
|
+
prepareInfoLog,
|
|
110
|
+
prepareErrorLog,
|
|
111
|
+
prepareAlertLog,
|
|
112
|
+
prepareDebugLog,
|
|
113
|
+
prepareWarnLog,
|
|
114
|
+
setModule,
|
|
115
|
+
};
|
|
@@ -1,145 +1,145 @@
|
|
|
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
|
-
*/
|
|
5
|
-
|
|
6
|
-
const { CloudTasksClient } = require('@google-cloud/tasks');
|
|
7
|
-
const { v4: uuidv4 } = require('uuid');
|
|
8
|
-
|
|
9
|
-
const PLAYO_LOGGER = require("@playo/logger");
|
|
10
|
-
const LOGGER = new PLAYO_LOGGER("playo-message-publisher");
|
|
11
|
-
|
|
12
|
-
const client = new CloudTasksClient();
|
|
13
|
-
|
|
14
|
-
const PROJECT = process.env.GCP_PROJECT;
|
|
15
|
-
const LOCATION = process.env.GCP_LOCATION;
|
|
16
|
-
|
|
17
|
-
if (!PROJECT || !LOCATION) {
|
|
18
|
-
console.warn('You need to pass - GCP_PROJECT, GCP_LOCATION in env for message queue to work!');
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const { google } = require('googleapis');
|
|
22
|
-
const cloudtasks = google.cloudtasks('v2');
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
*
|
|
26
|
-
* @param {String} queueId - Name of the queue being targeted
|
|
27
|
-
* @param {Object} payload - {
|
|
28
|
-
* httpMethod: String <POST, GET, PUT>
|
|
29
|
-
* url: String,
|
|
30
|
-
* headers: Object,
|
|
31
|
-
* body: Object, // not needed if GET request
|
|
32
|
-
* ttl: Optional, date in utc format, consumer will check if the ttl has been exceeded or not and then will respond accordingly
|
|
33
|
-
* ...
|
|
34
|
-
*
|
|
35
|
-
* }
|
|
36
|
-
* @param {Object} messageProcessingProperties - {
|
|
37
|
-
* delay
|
|
38
|
-
* }
|
|
39
|
-
* @param {String} taskId -used as de-duplication key
|
|
40
|
-
* @param {String} requestId
|
|
41
|
-
* @returns {}
|
|
42
|
-
*/
|
|
43
|
-
async function createHttpTask(queueId, payload, messageProcessingProperties, taskId, requestId) {
|
|
44
|
-
|
|
45
|
-
try {
|
|
46
|
-
const project = PROJECT;
|
|
47
|
-
const queue = queueId;
|
|
48
|
-
const location = LOCATION;
|
|
49
|
-
|
|
50
|
-
const { delay = 0 } = messageProcessingProperties
|
|
51
|
-
|
|
52
|
-
const parent = client.queuePath(project, location, queue);
|
|
53
|
-
|
|
54
|
-
const task = {
|
|
55
|
-
httpRequest: payload,
|
|
56
|
-
name: ''
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
task.name = getTaskId(project, location, queue, taskId);
|
|
60
|
-
|
|
61
|
-
if (payload.body) {
|
|
62
|
-
task.httpRequest.body = Buffer.from(payload.body).toString('base64');
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (delay) {
|
|
66
|
-
// The time when the task is scheduled to be attempted.
|
|
67
|
-
task.scheduleTime = {
|
|
68
|
-
seconds: delay + Date.now() / 1000,
|
|
69
|
-
};
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
LOGGER.info(requestId, task, `Sending task to queue ${queueId}}`)
|
|
73
|
-
|
|
74
|
-
const request = { parent: parent, task: task };
|
|
75
|
-
const [response] = await client.createTask(request);
|
|
76
|
-
|
|
77
|
-
LOGGER.info(requestId, { name: response.name, response }, `Added task to queue ${queueId}}`);
|
|
78
|
-
return response;
|
|
79
|
-
} catch (err) {
|
|
80
|
-
if (err && err.code === 6) { // Idempotent request - assume sucess when the task being tried to save is already there!
|
|
81
|
-
return Promise.resolve('success!');
|
|
82
|
-
}
|
|
83
|
-
throw err;
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Returns a unique string for name of a task in task queues, to be used for task de-duplication purpose.
|
|
89
|
-
* @param {String} projectId
|
|
90
|
-
* @param {String} locationId
|
|
91
|
-
* @param {String} queueId
|
|
92
|
-
* @param {String} taskId
|
|
93
|
-
* @returns {String}
|
|
94
|
-
*/
|
|
95
|
-
const getTaskId = (projectId, locationId, queueId, taskId = uuidv4()) => {
|
|
96
|
-
return `projects/${projectId}/locations/${locationId}/queues/${queueId}/tasks/${taskId}`;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
let authClient;
|
|
100
|
-
/**
|
|
101
|
-
* https://cloud.google.com/tasks/docs/reference/rest/v2/projects.locations.queues.tasks/delete
|
|
102
|
-
*
|
|
103
|
-
*/
|
|
104
|
-
|
|
105
|
-
(async () => {
|
|
106
|
-
authClient = await authorize();
|
|
107
|
-
})();
|
|
108
|
-
|
|
109
|
-
const deleteHttpTask = async (queueId, taskId) => {
|
|
110
|
-
if (!queueId || !taskId) {
|
|
111
|
-
throw new Error('Both queueId, and taskId are required to delete a task!;')
|
|
112
|
-
}
|
|
113
|
-
const requestData = {
|
|
114
|
-
name: getTaskId(PROJECT, LOCATION, queueId, taskId),
|
|
115
|
-
auth: authClient
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
try {
|
|
119
|
-
await cloudtasks.projects.locations.queues.tasks.delete(requestData);
|
|
120
|
-
} catch (err) {
|
|
121
|
-
/*
|
|
122
|
-
Error response schema -
|
|
123
|
-
Naturally if GCP response is that task didn't exist while we were trying to delete it, then we should not
|
|
124
|
-
pass back the error to calle function and resume operations as normal. As delete operation is atomic and idempotent.
|
|
125
|
-
*/
|
|
126
|
-
if (err.response.data && err.response.data.error && err.response.data.error.message === 'Requested entity was not found.') {
|
|
127
|
-
return Promise.resolve('success!');
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
throw err;
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
async function authorize() {
|
|
135
|
-
const auth = new google.auth.GoogleAuth({
|
|
136
|
-
scopes: ['https://www.googleapis.com/auth/cloud-platform']
|
|
137
|
-
});
|
|
138
|
-
return await auth.getClient();
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
module.exports = {
|
|
142
|
-
createHttpTask,
|
|
143
|
-
getTaskId,
|
|
144
|
-
deleteHttpTask
|
|
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
|
+
*/
|
|
5
|
+
|
|
6
|
+
const { CloudTasksClient } = require('@google-cloud/tasks');
|
|
7
|
+
const { v4: uuidv4 } = require('uuid');
|
|
8
|
+
|
|
9
|
+
const PLAYO_LOGGER = require("@playo/logger");
|
|
10
|
+
const LOGGER = new PLAYO_LOGGER("playo-message-publisher");
|
|
11
|
+
|
|
12
|
+
const client = new CloudTasksClient();
|
|
13
|
+
|
|
14
|
+
const PROJECT = process.env.GCP_PROJECT;
|
|
15
|
+
const LOCATION = process.env.GCP_LOCATION;
|
|
16
|
+
|
|
17
|
+
if (!PROJECT || !LOCATION) {
|
|
18
|
+
console.warn('You need to pass - GCP_PROJECT, GCP_LOCATION in env for message queue to work!');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const { google } = require('googleapis');
|
|
22
|
+
const cloudtasks = google.cloudtasks('v2');
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
*
|
|
26
|
+
* @param {String} queueId - Name of the queue being targeted
|
|
27
|
+
* @param {Object} payload - {
|
|
28
|
+
* httpMethod: String <POST, GET, PUT>
|
|
29
|
+
* url: String,
|
|
30
|
+
* headers: Object,
|
|
31
|
+
* body: Object, // not needed if GET request
|
|
32
|
+
* ttl: Optional, date in utc format, consumer will check if the ttl has been exceeded or not and then will respond accordingly
|
|
33
|
+
* ...
|
|
34
|
+
*
|
|
35
|
+
* }
|
|
36
|
+
* @param {Object} messageProcessingProperties - {
|
|
37
|
+
* delay
|
|
38
|
+
* }
|
|
39
|
+
* @param {String} taskId -used as de-duplication key
|
|
40
|
+
* @param {String} requestId
|
|
41
|
+
* @returns {}
|
|
42
|
+
*/
|
|
43
|
+
async function createHttpTask(queueId, payload, messageProcessingProperties, taskId, requestId) {
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
const project = PROJECT;
|
|
47
|
+
const queue = queueId;
|
|
48
|
+
const location = LOCATION;
|
|
49
|
+
|
|
50
|
+
const { delay = 0 } = messageProcessingProperties
|
|
51
|
+
|
|
52
|
+
const parent = client.queuePath(project, location, queue);
|
|
53
|
+
|
|
54
|
+
const task = {
|
|
55
|
+
httpRequest: payload,
|
|
56
|
+
name: ''
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
task.name = getTaskId(project, location, queue, taskId);
|
|
60
|
+
|
|
61
|
+
if (payload.body) {
|
|
62
|
+
task.httpRequest.body = Buffer.from(payload.body).toString('base64');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (delay) {
|
|
66
|
+
// The time when the task is scheduled to be attempted.
|
|
67
|
+
task.scheduleTime = {
|
|
68
|
+
seconds: delay + Date.now() / 1000,
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
LOGGER.info(requestId, task, `Sending task to queue ${queueId}}`)
|
|
73
|
+
|
|
74
|
+
const request = { parent: parent, task: task };
|
|
75
|
+
const [response] = await client.createTask(request);
|
|
76
|
+
|
|
77
|
+
LOGGER.info(requestId, { name: response.name, response }, `Added task to queue ${queueId}}`);
|
|
78
|
+
return response;
|
|
79
|
+
} catch (err) {
|
|
80
|
+
if (err && err.code === 6) { // Idempotent request - assume sucess when the task being tried to save is already there!
|
|
81
|
+
return Promise.resolve('success!');
|
|
82
|
+
}
|
|
83
|
+
throw err;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Returns a unique string for name of a task in task queues, to be used for task de-duplication purpose.
|
|
89
|
+
* @param {String} projectId
|
|
90
|
+
* @param {String} locationId
|
|
91
|
+
* @param {String} queueId
|
|
92
|
+
* @param {String} taskId
|
|
93
|
+
* @returns {String}
|
|
94
|
+
*/
|
|
95
|
+
const getTaskId = (projectId, locationId, queueId, taskId = uuidv4()) => {
|
|
96
|
+
return `projects/${projectId}/locations/${locationId}/queues/${queueId}/tasks/${taskId}`;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
let authClient;
|
|
100
|
+
/**
|
|
101
|
+
* https://cloud.google.com/tasks/docs/reference/rest/v2/projects.locations.queues.tasks/delete
|
|
102
|
+
*
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
(async () => {
|
|
106
|
+
authClient = await authorize();
|
|
107
|
+
})();
|
|
108
|
+
|
|
109
|
+
const deleteHttpTask = async (queueId, taskId) => {
|
|
110
|
+
if (!queueId || !taskId) {
|
|
111
|
+
throw new Error('Both queueId, and taskId are required to delete a task!;')
|
|
112
|
+
}
|
|
113
|
+
const requestData = {
|
|
114
|
+
name: getTaskId(PROJECT, LOCATION, queueId, taskId),
|
|
115
|
+
auth: authClient
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
await cloudtasks.projects.locations.queues.tasks.delete(requestData);
|
|
120
|
+
} catch (err) {
|
|
121
|
+
/*
|
|
122
|
+
Error response schema -
|
|
123
|
+
Naturally if GCP response is that task didn't exist while we were trying to delete it, then we should not
|
|
124
|
+
pass back the error to calle function and resume operations as normal. As delete operation is atomic and idempotent.
|
|
125
|
+
*/
|
|
126
|
+
if (err.response.data && err.response.data.error && err.response.data.error.message === 'Requested entity was not found.') {
|
|
127
|
+
return Promise.resolve('success!');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
throw err;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
async function authorize() {
|
|
135
|
+
const auth = new google.auth.GoogleAuth({
|
|
136
|
+
scopes: ['https://www.googleapis.com/auth/cloud-platform']
|
|
137
|
+
});
|
|
138
|
+
return await auth.getClient();
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
module.exports = {
|
|
142
|
+
createHttpTask,
|
|
143
|
+
getTaskId,
|
|
144
|
+
deleteHttpTask
|
|
145
145
|
};
|
package/lib/middleware.js
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
const { v4: uuidv4 } = require('uuid');
|
|
3
|
-
const Logger = require('./logger')
|
|
4
|
-
|
|
5
|
-
const playoServiceRequestInterceptor = (req, res, next, service = 'microservice-name') => {
|
|
6
|
-
const id = req.headers["x-request-id"] || uuidv4();
|
|
7
|
-
const start = Date.now();
|
|
8
|
-
let userId = 'internal-request';
|
|
9
|
-
if (req.headers.userinfo) {
|
|
10
|
-
try {
|
|
11
|
-
userId = JSON.parse(req.headers.userinfo).userId
|
|
12
|
-
} catch (error) {
|
|
13
|
-
Logger.prepareInfoLog(id, { userinfo: req.headers.userinfo }, "Malformed userInfo in headers detected");
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
const url = req.url;
|
|
17
|
-
const requestDetails = {
|
|
18
|
-
method: req.method,
|
|
19
|
-
params: req.params,
|
|
20
|
-
query: req.query,
|
|
21
|
-
url,
|
|
22
|
-
originalUrl: req.originalUrl,
|
|
23
|
-
service
|
|
24
|
-
};
|
|
25
|
-
if (req.body && JSON.stringify(req.body).length < 10000) { // 0.1 mb
|
|
26
|
-
requestDetails['body'] = req.body || {};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
Logger.prepareInfoLog(id, requestDetails, "Incoming request");
|
|
30
|
-
|
|
31
|
-
res.on('finish', function () {
|
|
32
|
-
const duration = Date.now() - start;
|
|
33
|
-
const status = res.statusCode;
|
|
34
|
-
Logger.prepareInfoLog(id, { status, duration, service, url, userId }, "Outgoing request - finish event");
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
res.on('close', function () {
|
|
38
|
-
const duration = Date.now() - start;
|
|
39
|
-
const status = res.statusCode;
|
|
40
|
-
Logger.prepareInfoLog(id, { status, duration, service, url, userId }, "Outgoing request - close event");
|
|
41
|
-
});
|
|
42
|
-
next();
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
module.exports = {
|
|
46
|
-
playoServiceRequestInterceptor
|
|
1
|
+
|
|
2
|
+
const { v4: uuidv4 } = require('uuid');
|
|
3
|
+
const Logger = require('./logger')
|
|
4
|
+
|
|
5
|
+
const playoServiceRequestInterceptor = (req, res, next, service = 'microservice-name') => {
|
|
6
|
+
const id = req.headers["x-request-id"] || uuidv4();
|
|
7
|
+
const start = Date.now();
|
|
8
|
+
let userId = 'internal-request';
|
|
9
|
+
if (req.headers.userinfo) {
|
|
10
|
+
try {
|
|
11
|
+
userId = JSON.parse(req.headers.userinfo).userId
|
|
12
|
+
} catch (error) {
|
|
13
|
+
Logger.prepareInfoLog(id, { userinfo: req.headers.userinfo }, "Malformed userInfo in headers detected");
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const url = req.url;
|
|
17
|
+
const requestDetails = {
|
|
18
|
+
method: req.method,
|
|
19
|
+
params: req.params,
|
|
20
|
+
query: req.query,
|
|
21
|
+
url,
|
|
22
|
+
originalUrl: req.originalUrl,
|
|
23
|
+
service
|
|
24
|
+
};
|
|
25
|
+
if (req.body && JSON.stringify(req.body).length < 10000) { // 0.1 mb
|
|
26
|
+
requestDetails['body'] = req.body || {};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
Logger.prepareInfoLog(id, requestDetails, "Incoming request");
|
|
30
|
+
|
|
31
|
+
res.on('finish', function () {
|
|
32
|
+
const duration = Date.now() - start;
|
|
33
|
+
const status = res.statusCode;
|
|
34
|
+
Logger.prepareInfoLog(id, { status, duration, service, url, userId }, "Outgoing request - finish event");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
res.on('close', function () {
|
|
38
|
+
const duration = Date.now() - start;
|
|
39
|
+
const status = res.statusCode;
|
|
40
|
+
Logger.prepareInfoLog(id, { status, duration, service, url, userId }, "Outgoing request - close event");
|
|
41
|
+
});
|
|
42
|
+
next();
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
module.exports = {
|
|
46
|
+
playoServiceRequestInterceptor
|
|
47
47
|
}
|