@mhubcloud/tms_core 3.7.0 → 3.7.1-beta.1
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/dist/index.js +66 -45
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -108,14 +108,24 @@ function logEvent(publisher2, event, region, status, payload, id) {
|
|
|
108
108
|
if (status || status === 0) {
|
|
109
109
|
if (status >= 200 && status < 300) {
|
|
110
110
|
aiClient?.trackMetric({
|
|
111
|
-
name: `
|
|
112
|
-
value: 1
|
|
111
|
+
name: `DispatchSuccess`,
|
|
112
|
+
value: 1,
|
|
113
|
+
properties: {
|
|
114
|
+
publisher: publisher2,
|
|
115
|
+
event,
|
|
116
|
+
region
|
|
117
|
+
}
|
|
113
118
|
});
|
|
114
119
|
message = "Event dispatched to platform";
|
|
115
120
|
} else {
|
|
116
121
|
aiClient?.trackMetric({
|
|
117
|
-
name: `
|
|
118
|
-
value: 1
|
|
122
|
+
name: `DispatchFail`,
|
|
123
|
+
value: 1,
|
|
124
|
+
properties: {
|
|
125
|
+
publisher: publisher2,
|
|
126
|
+
event,
|
|
127
|
+
region
|
|
128
|
+
}
|
|
119
129
|
});
|
|
120
130
|
message = "Event dispatch failed";
|
|
121
131
|
}
|
|
@@ -133,10 +143,14 @@ function logEvent(publisher2, event, region, status, payload, id) {
|
|
|
133
143
|
properties = removeUndefined(properties);
|
|
134
144
|
logMessage(1, message, properties);
|
|
135
145
|
}
|
|
136
|
-
function logException(exception) {
|
|
146
|
+
function logException(exception, publisher2) {
|
|
137
147
|
aiClient?.trackMetric({
|
|
138
|
-
name: `Error
|
|
139
|
-
value: 1
|
|
148
|
+
name: `Error`,
|
|
149
|
+
value: 1,
|
|
150
|
+
properties: {
|
|
151
|
+
type: exception.name,
|
|
152
|
+
publisher: publisher2 ?? "Other"
|
|
153
|
+
}
|
|
140
154
|
});
|
|
141
155
|
aiClient?.trackException({ exception });
|
|
142
156
|
}
|
|
@@ -497,7 +511,7 @@ async function logToCosmosDb(message) {
|
|
|
497
511
|
try {
|
|
498
512
|
await cosmosClient.database(cosmosDatabaseName).container(cosmosContainerName).items.create(message);
|
|
499
513
|
} catch (e) {
|
|
500
|
-
logException(e);
|
|
514
|
+
logException(e, "Broker");
|
|
501
515
|
}
|
|
502
516
|
}
|
|
503
517
|
function parseScheduleOptions(options) {
|
|
@@ -516,8 +530,9 @@ function parseScheduleOptions(options) {
|
|
|
516
530
|
0
|
|
517
531
|
);
|
|
518
532
|
} else if ("scheduleInterval" in options && "startHour" in options) {
|
|
519
|
-
const hour = now.getHours();
|
|
520
|
-
const
|
|
533
|
+
const hour = now.getHours() + now.getMinutes() / 60;
|
|
534
|
+
const offset = options.startHour % options.scheduleInterval;
|
|
535
|
+
const nextHour = Math.ceil((hour - offset) / options.scheduleInterval) * options.scheduleInterval + offset;
|
|
521
536
|
dispatchTime = new Date(
|
|
522
537
|
now.getFullYear(),
|
|
523
538
|
now.getMonth(),
|
|
@@ -529,7 +544,7 @@ function parseScheduleOptions(options) {
|
|
|
529
544
|
);
|
|
530
545
|
if (nextHour >= 24) {
|
|
531
546
|
dispatchTime.setDate(dispatchTime.getDate() + 1);
|
|
532
|
-
dispatchTime.setHours(options.startHour, options.startMinute);
|
|
547
|
+
dispatchTime.setHours(options.startHour, options.startMinute ?? 0);
|
|
533
548
|
}
|
|
534
549
|
} else {
|
|
535
550
|
throw new Error("Invalid scheduling configuration");
|
|
@@ -585,13 +600,13 @@ async function defaultPublisher(azureFunctionContext, serviceBusMessage, source,
|
|
|
585
600
|
if (result.status === "rejected") {
|
|
586
601
|
if (result.reason instanceof Error) {
|
|
587
602
|
const formattedError = formatError(result.reason);
|
|
588
|
-
logException(new Error(formattedError.message));
|
|
603
|
+
logException(new Error(formattedError.message), source);
|
|
589
604
|
throw formattedError;
|
|
590
605
|
} else {
|
|
591
606
|
const formattedError = formatError(
|
|
592
607
|
typeof result.reason === "object" ? JSON.stringify(result.reason) : result.reason
|
|
593
608
|
);
|
|
594
|
-
logException(new Error(formattedError.message));
|
|
609
|
+
logException(new Error(formattedError.message), source);
|
|
595
610
|
throw formattedError;
|
|
596
611
|
}
|
|
597
612
|
}
|
|
@@ -615,13 +630,15 @@ async function processMessage(message, log2, platformPublisher, trigger) {
|
|
|
615
630
|
const failedToPublishAll = false;
|
|
616
631
|
for (const eventPublisher of publishers) {
|
|
617
632
|
const mapped = await eventPublisher.map(message, log2);
|
|
633
|
+
const name = message.event.name?.toLowerCase();
|
|
634
|
+
const code = message.event.code?.toLowerCase();
|
|
618
635
|
const res = await publishMessage(
|
|
619
636
|
log2,
|
|
620
637
|
mapped,
|
|
621
638
|
eventPublisher.config,
|
|
622
639
|
platformPublisher,
|
|
623
640
|
{
|
|
624
|
-
event:
|
|
641
|
+
event: name ? name === code ? name : `${code}-${name}` : code,
|
|
625
642
|
region: message.event.region,
|
|
626
643
|
id: message.messageId
|
|
627
644
|
}
|
|
@@ -632,7 +649,8 @@ async function processMessage(message, log2, platformPublisher, trigger) {
|
|
|
632
649
|
logException(
|
|
633
650
|
new Error(
|
|
634
651
|
`The message has not been published. Source: ${platformPublisher.source}`
|
|
635
|
-
)
|
|
652
|
+
),
|
|
653
|
+
platformPublisher.source
|
|
636
654
|
);
|
|
637
655
|
}
|
|
638
656
|
}
|
|
@@ -640,7 +658,8 @@ async function processMessage(message, log2, platformPublisher, trigger) {
|
|
|
640
658
|
logException(
|
|
641
659
|
new Error(
|
|
642
660
|
"The message has not been published to any publisher configuration"
|
|
643
|
-
)
|
|
661
|
+
),
|
|
662
|
+
platformPublisher.source
|
|
644
663
|
);
|
|
645
664
|
return false;
|
|
646
665
|
}
|
|
@@ -806,8 +825,7 @@ var googleAds = {
|
|
|
806
825
|
const body = {
|
|
807
826
|
conversions: [data],
|
|
808
827
|
partialFailure: true,
|
|
809
|
-
validateOnly: false
|
|
810
|
-
debugEnabled: false
|
|
828
|
+
validateOnly: false
|
|
811
829
|
};
|
|
812
830
|
const requestOptions = {
|
|
813
831
|
method: "POST",
|
|
@@ -819,7 +837,7 @@ var googleAds = {
|
|
|
819
837
|
},
|
|
820
838
|
body: JSON.stringify(body)
|
|
821
839
|
};
|
|
822
|
-
const baseUrl = `https://googleads.googleapis.com/
|
|
840
|
+
const baseUrl = `https://googleads.googleapis.com/v24/customers/${config.customerId}:uploadClickConversions`;
|
|
823
841
|
const response = await fetch(baseUrl, requestOptions);
|
|
824
842
|
const responseJson = await response.json();
|
|
825
843
|
const knownErrors = [
|
|
@@ -863,34 +881,37 @@ var googleAds = {
|
|
|
863
881
|
}
|
|
864
882
|
if (!tokenPromise) {
|
|
865
883
|
tokenPromise = (async () => {
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
+
try {
|
|
885
|
+
const tokenResponse = await fetch(
|
|
886
|
+
`https://oauth2.googleapis.com/token`,
|
|
887
|
+
{
|
|
888
|
+
method: "POST",
|
|
889
|
+
headers: {
|
|
890
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
891
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
892
|
+
},
|
|
893
|
+
body: new URLSearchParams({
|
|
894
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
895
|
+
client_id: config.client.id,
|
|
896
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
897
|
+
client_secret: config.client.secret,
|
|
898
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
899
|
+
refreshToken: config.refreshToken,
|
|
900
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
901
|
+
grant_type: "refresh_token"
|
|
902
|
+
})
|
|
903
|
+
}
|
|
904
|
+
);
|
|
905
|
+
if (!tokenResponse.ok) {
|
|
906
|
+
throw new Error("Access token fetch failed");
|
|
884
907
|
}
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
908
|
+
const parsedTokenResponse = await tokenResponse.json();
|
|
909
|
+
const createdAt = Date.now();
|
|
910
|
+
tokenCache = { token: parsedTokenResponse.access_token, createdAt };
|
|
911
|
+
return tokenCache;
|
|
912
|
+
} finally {
|
|
913
|
+
tokenPromise = null;
|
|
888
914
|
}
|
|
889
|
-
const parsedTokenResponse = await tokenResponse.json();
|
|
890
|
-
const createdAt = Date.now();
|
|
891
|
-
tokenCache = { token: parsedTokenResponse.access_token, createdAt };
|
|
892
|
-
tokenPromise = null;
|
|
893
|
-
return tokenCache;
|
|
894
915
|
})();
|
|
895
916
|
}
|
|
896
917
|
return tokenPromise;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mhubcloud/tms_core",
|
|
3
|
-
"version": "3.7.
|
|
3
|
+
"version": "3.7.1-beta.1",
|
|
4
4
|
"description": "Core function for mHub Cloud TMS publishers",
|
|
5
5
|
"homepage": "https://mhubcloud.com/",
|
|
6
6
|
"license": "Apache 2.0",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"conversion API"
|
|
17
17
|
],
|
|
18
18
|
"scripts": {
|
|
19
|
-
"test": "mocha ./tests
|
|
19
|
+
"test": "mocha ./tests/*.js --local-settings=./local.settings.json --timeout 100000",
|
|
20
20
|
"dev": "ts-node index.ts",
|
|
21
21
|
"build": "tsup ./src/index.ts --format cjs --target node22"
|
|
22
22
|
},
|