@terreno/comms 56.0.0-beta.2
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/LICENSE +21 -0
- package/biome.jsonc +17 -0
- package/bunfig.toml +5 -0
- package/dist/adapters/console.d.ts +30 -0
- package/dist/adapters/console.js +122 -0
- package/dist/commsApp.d.ts +14 -0
- package/dist/commsApp.js +37 -0
- package/dist/commsService.d.ts +20 -0
- package/dist/commsService.js +386 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +19 -0
- package/dist/modelTypes.d.ts +53 -0
- package/dist/modelTypes.js +2 -0
- package/dist/models/commsMessage.d.ts +2 -0
- package/dist/models/commsMessage.js +119 -0
- package/dist/models/pushToken.d.ts +2 -0
- package/dist/models/pushToken.js +56 -0
- package/dist/routes/commsExplorer.d.ts +7 -0
- package/dist/routes/commsExplorer.js +147 -0
- package/dist/routes/pushTokens.d.ts +7 -0
- package/dist/routes/pushTokens.js +292 -0
- package/dist/templates.d.ts +10 -0
- package/dist/templates.js +32 -0
- package/dist/types.d.ts +78 -0
- package/dist/types.js +2 -0
- package/package.json +55 -0
- package/src/adapters/console.test.ts +227 -0
- package/src/adapters/console.ts +87 -0
- package/src/commsApp.test.ts +448 -0
- package/src/commsApp.ts +48 -0
- package/src/commsService.test.ts +570 -0
- package/src/commsService.ts +293 -0
- package/src/index.ts +45 -0
- package/src/modelTypes.ts +78 -0
- package/src/models/commsMessage.test.ts +53 -0
- package/src/models/commsMessage.ts +97 -0
- package/src/models/pushToken.test.ts +45 -0
- package/src/models/pushToken.ts +69 -0
- package/src/routes/commsExplorer.ts +114 -0
- package/src/routes/pushTokens.ts +265 -0
- package/src/templates.test.ts +37 -0
- package/src/templates.ts +31 -0
- package/src/tests/bunSetup.ts +14 -0
- package/src/types.ts +93 -0
- package/tsconfig.json +32 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { ConsoleMailProvider, ConsolePushProvider, ConsoleSmsProvider, ConsoleVerificationProvider, } from "./adapters/console";
|
|
2
|
+
export type { CommsAppOptions } from "./commsApp";
|
|
3
|
+
export { CommsApp, getCommsService } from "./commsApp";
|
|
4
|
+
export { CommsService } from "./commsService";
|
|
5
|
+
export { CommsMessage } from "./models/commsMessage";
|
|
6
|
+
export { PushToken } from "./models/pushToken";
|
|
7
|
+
export type { CommsMessageDocument, CommsMessageMethods, CommsMessageModel, CommsMessageSchema, CommsMessageStatics, LogSendParams, PushTokenDocument, PushTokenMethods, PushTokenModel, PushTokenPlatform, PushTokenSchema, PushTokenStatics, } from "./modelTypes";
|
|
8
|
+
export type { MessageTemplate, RenderTemplateOptions } from "./templates";
|
|
9
|
+
export { renderTemplate } from "./templates";
|
|
10
|
+
export type { CheckVerificationOptions, CommsChannel, CommsMessageStatus, CommsOptions, DeliveryEvent, MailMessage, MailProvider, PushMessage, PushProvider, SendPushToUserMessage, SendResult, SmsMessage, SmsProvider, StartVerificationOptions, VerificationProvider, VerificationResult, } from "./types";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.renderTemplate = exports.PushToken = exports.CommsMessage = exports.CommsService = exports.getCommsService = exports.CommsApp = exports.ConsoleVerificationProvider = exports.ConsoleSmsProvider = exports.ConsolePushProvider = exports.ConsoleMailProvider = void 0;
|
|
4
|
+
var console_1 = require("./adapters/console");
|
|
5
|
+
Object.defineProperty(exports, "ConsoleMailProvider", { enumerable: true, get: function () { return console_1.ConsoleMailProvider; } });
|
|
6
|
+
Object.defineProperty(exports, "ConsolePushProvider", { enumerable: true, get: function () { return console_1.ConsolePushProvider; } });
|
|
7
|
+
Object.defineProperty(exports, "ConsoleSmsProvider", { enumerable: true, get: function () { return console_1.ConsoleSmsProvider; } });
|
|
8
|
+
Object.defineProperty(exports, "ConsoleVerificationProvider", { enumerable: true, get: function () { return console_1.ConsoleVerificationProvider; } });
|
|
9
|
+
var commsApp_1 = require("./commsApp");
|
|
10
|
+
Object.defineProperty(exports, "CommsApp", { enumerable: true, get: function () { return commsApp_1.CommsApp; } });
|
|
11
|
+
Object.defineProperty(exports, "getCommsService", { enumerable: true, get: function () { return commsApp_1.getCommsService; } });
|
|
12
|
+
var commsService_1 = require("./commsService");
|
|
13
|
+
Object.defineProperty(exports, "CommsService", { enumerable: true, get: function () { return commsService_1.CommsService; } });
|
|
14
|
+
var commsMessage_1 = require("./models/commsMessage");
|
|
15
|
+
Object.defineProperty(exports, "CommsMessage", { enumerable: true, get: function () { return commsMessage_1.CommsMessage; } });
|
|
16
|
+
var pushToken_1 = require("./models/pushToken");
|
|
17
|
+
Object.defineProperty(exports, "PushToken", { enumerable: true, get: function () { return pushToken_1.PushToken; } });
|
|
18
|
+
var templates_1 = require("./templates");
|
|
19
|
+
Object.defineProperty(exports, "renderTemplate", { enumerable: true, get: function () { return templates_1.renderTemplate; } });
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { FindExactlyOnePlugin, FindOneOrNonePlugin, HasUpsert } from "@terreno/api";
|
|
2
|
+
import type mongoose from "mongoose";
|
|
3
|
+
import type { CommsChannel, CommsMessageStatus } from "./types";
|
|
4
|
+
export type CommsMessageMethods = Record<never, never>;
|
|
5
|
+
export interface CommsMessageDocument extends mongoose.Document<mongoose.Types.ObjectId>, CommsMessageMethods {
|
|
6
|
+
channel: CommsChannel;
|
|
7
|
+
created: Date;
|
|
8
|
+
deleted: boolean;
|
|
9
|
+
error?: string;
|
|
10
|
+
metadata?: Record<string, unknown>;
|
|
11
|
+
provider: string;
|
|
12
|
+
providerMessageId?: string;
|
|
13
|
+
status: CommsMessageStatus;
|
|
14
|
+
subject?: string;
|
|
15
|
+
to: string;
|
|
16
|
+
updated: Date;
|
|
17
|
+
userId?: mongoose.Types.ObjectId;
|
|
18
|
+
}
|
|
19
|
+
export interface LogSendParams {
|
|
20
|
+
channel: CommsChannel;
|
|
21
|
+
error?: string;
|
|
22
|
+
metadata?: Record<string, unknown>;
|
|
23
|
+
provider: string;
|
|
24
|
+
providerMessageId?: string;
|
|
25
|
+
status: CommsMessageStatus;
|
|
26
|
+
subject?: string;
|
|
27
|
+
to: string;
|
|
28
|
+
userId?: mongoose.Types.ObjectId | string;
|
|
29
|
+
}
|
|
30
|
+
export interface CommsMessageStatics extends FindExactlyOnePlugin<CommsMessageDocument>, FindOneOrNonePlugin<CommsMessageDocument> {
|
|
31
|
+
logSend(params: LogSendParams): Promise<CommsMessageDocument | null>;
|
|
32
|
+
}
|
|
33
|
+
export interface CommsMessageModel extends mongoose.Model<CommsMessageDocument>, CommsMessageStatics {
|
|
34
|
+
}
|
|
35
|
+
export type CommsMessageSchema = mongoose.Schema<CommsMessageDocument, CommsMessageModel, CommsMessageMethods>;
|
|
36
|
+
export type PushTokenPlatform = "android" | "ios" | "web";
|
|
37
|
+
export type PushTokenMethods = Record<never, never>;
|
|
38
|
+
export interface PushTokenDocument extends mongoose.Document<mongoose.Types.ObjectId>, PushTokenMethods {
|
|
39
|
+
active: boolean;
|
|
40
|
+
created: Date;
|
|
41
|
+
deleted: boolean;
|
|
42
|
+
deviceId?: string;
|
|
43
|
+
lastSeenAt: Date;
|
|
44
|
+
platform: PushTokenPlatform;
|
|
45
|
+
token: string;
|
|
46
|
+
updated: Date;
|
|
47
|
+
userId: mongoose.Types.ObjectId;
|
|
48
|
+
}
|
|
49
|
+
export interface PushTokenStatics extends FindExactlyOnePlugin<PushTokenDocument>, FindOneOrNonePlugin<PushTokenDocument>, HasUpsert<PushTokenDocument> {
|
|
50
|
+
}
|
|
51
|
+
export interface PushTokenModel extends mongoose.Model<PushTokenDocument>, PushTokenStatics {
|
|
52
|
+
}
|
|
53
|
+
export type PushTokenSchema = mongoose.Schema<PushTokenDocument, PushTokenModel, PushTokenMethods>;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.CommsMessage = void 0;
|
|
43
|
+
var api_1 = require("@terreno/api");
|
|
44
|
+
var mongoose_1 = __importDefault(require("mongoose"));
|
|
45
|
+
var commsMessageSchema = new mongoose_1.default.Schema({
|
|
46
|
+
channel: {
|
|
47
|
+
description: "Communication channel used for the send attempt",
|
|
48
|
+
enum: ["mail", "push", "sms", "verification"],
|
|
49
|
+
required: true,
|
|
50
|
+
type: String,
|
|
51
|
+
},
|
|
52
|
+
error: {
|
|
53
|
+
description: "Provider error returned for a failed send attempt",
|
|
54
|
+
type: String,
|
|
55
|
+
},
|
|
56
|
+
metadata: {
|
|
57
|
+
description: "Additional metadata associated with the send attempt",
|
|
58
|
+
type: mongoose_1.default.Schema.Types.Mixed,
|
|
59
|
+
},
|
|
60
|
+
provider: {
|
|
61
|
+
description: "Identifier of the provider handling the send attempt",
|
|
62
|
+
minlength: 1,
|
|
63
|
+
required: true,
|
|
64
|
+
type: String,
|
|
65
|
+
},
|
|
66
|
+
providerMessageId: {
|
|
67
|
+
description: "Provider-assigned identifier for delivery tracking",
|
|
68
|
+
index: true,
|
|
69
|
+
type: String,
|
|
70
|
+
},
|
|
71
|
+
status: {
|
|
72
|
+
description: "Current delivery status of the communication",
|
|
73
|
+
enum: ["bounced", "delivered", "failed", "sent"],
|
|
74
|
+
required: true,
|
|
75
|
+
type: String,
|
|
76
|
+
},
|
|
77
|
+
subject: {
|
|
78
|
+
description: "Mail subject when the communication channel is mail",
|
|
79
|
+
type: String,
|
|
80
|
+
},
|
|
81
|
+
to: {
|
|
82
|
+
description: "Recipient identifier for the communication",
|
|
83
|
+
required: true,
|
|
84
|
+
type: String,
|
|
85
|
+
},
|
|
86
|
+
userId: {
|
|
87
|
+
description: "User associated with the communication",
|
|
88
|
+
ref: "User",
|
|
89
|
+
type: mongoose_1.default.Schema.Types.ObjectId,
|
|
90
|
+
},
|
|
91
|
+
}, { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
|
|
92
|
+
commsMessageSchema.statics = {
|
|
93
|
+
logSend: function (params) {
|
|
94
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
95
|
+
var _a;
|
|
96
|
+
return __generator(this, function (_b) {
|
|
97
|
+
switch (_b.label) {
|
|
98
|
+
case 0:
|
|
99
|
+
_b.trys.push([0, 2, , 3]);
|
|
100
|
+
return [4 /*yield*/, this.create(params)];
|
|
101
|
+
case 1: return [2 /*return*/, _b.sent()];
|
|
102
|
+
case 2:
|
|
103
|
+
_a = _b.sent();
|
|
104
|
+
api_1.logger.warn("[comms] Failed to record communication send");
|
|
105
|
+
return [2 /*return*/, null];
|
|
106
|
+
case 3: return [2 /*return*/];
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
commsMessageSchema.plugin(api_1.createdUpdatedPlugin);
|
|
113
|
+
commsMessageSchema.plugin(api_1.isDeletedPlugin);
|
|
114
|
+
commsMessageSchema.plugin(api_1.findOneOrNone);
|
|
115
|
+
commsMessageSchema.plugin(api_1.findExactlyOne);
|
|
116
|
+
commsMessageSchema.index({ channel: 1, created: -1 });
|
|
117
|
+
commsMessageSchema.index({ created: -1, status: 1 });
|
|
118
|
+
commsMessageSchema.index({ created: -1, userId: 1 });
|
|
119
|
+
exports.CommsMessage = mongoose_1.default.model("CommsMessage", commsMessageSchema);
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.PushToken = void 0;
|
|
7
|
+
var api_1 = require("@terreno/api");
|
|
8
|
+
var luxon_1 = require("luxon");
|
|
9
|
+
var mongoose_1 = __importDefault(require("mongoose"));
|
|
10
|
+
var pushTokenSchema = new mongoose_1.default.Schema({
|
|
11
|
+
active: {
|
|
12
|
+
default: true,
|
|
13
|
+
description: "Whether the device token is available for push delivery",
|
|
14
|
+
type: Boolean,
|
|
15
|
+
},
|
|
16
|
+
deviceId: {
|
|
17
|
+
description: "Application-provided identifier for the device",
|
|
18
|
+
type: String,
|
|
19
|
+
},
|
|
20
|
+
lastSeenAt: {
|
|
21
|
+
default: function () { return luxon_1.DateTime.utc().toJSDate(); },
|
|
22
|
+
description: "Most recent time the device token was registered",
|
|
23
|
+
required: true,
|
|
24
|
+
type: Date,
|
|
25
|
+
},
|
|
26
|
+
platform: {
|
|
27
|
+
description: "Platform associated with the device token",
|
|
28
|
+
enum: ["android", "ios", "web"],
|
|
29
|
+
required: true,
|
|
30
|
+
type: String,
|
|
31
|
+
},
|
|
32
|
+
token: {
|
|
33
|
+
description: "Push provider token identifying the device",
|
|
34
|
+
minlength: 1,
|
|
35
|
+
required: true,
|
|
36
|
+
type: String,
|
|
37
|
+
unique: true,
|
|
38
|
+
},
|
|
39
|
+
userId: {
|
|
40
|
+
description: "User who owns the device token",
|
|
41
|
+
index: true,
|
|
42
|
+
ref: "User",
|
|
43
|
+
required: true,
|
|
44
|
+
type: mongoose_1.default.Schema.Types.ObjectId,
|
|
45
|
+
},
|
|
46
|
+
}, { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
|
|
47
|
+
pushTokenSchema.virtual("ownerId").get(function () {
|
|
48
|
+
return this.userId;
|
|
49
|
+
});
|
|
50
|
+
pushTokenSchema.plugin(api_1.createdUpdatedPlugin);
|
|
51
|
+
pushTokenSchema.plugin(api_1.isDeletedPlugin);
|
|
52
|
+
pushTokenSchema.plugin(api_1.findOneOrNone);
|
|
53
|
+
pushTokenSchema.plugin(api_1.findExactlyOne);
|
|
54
|
+
pushTokenSchema.plugin(api_1.upsertPlugin);
|
|
55
|
+
pushTokenSchema.index({ active: 1, userId: 1 });
|
|
56
|
+
exports.PushToken = mongoose_1.default.model("PushToken", pushTokenSchema);
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
39
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
40
|
+
if (!m) return o;
|
|
41
|
+
var i = m.call(o), r, ar = [], e;
|
|
42
|
+
try {
|
|
43
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
44
|
+
}
|
|
45
|
+
catch (error) { e = { error: error }; }
|
|
46
|
+
finally {
|
|
47
|
+
try {
|
|
48
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
49
|
+
}
|
|
50
|
+
finally { if (e) throw e.error; }
|
|
51
|
+
}
|
|
52
|
+
return ar;
|
|
53
|
+
};
|
|
54
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
55
|
+
exports.addCommsExplorerRoute = void 0;
|
|
56
|
+
var api_1 = require("@terreno/api");
|
|
57
|
+
var luxon_1 = require("luxon");
|
|
58
|
+
var commsMessage_1 = require("../models/commsMessage");
|
|
59
|
+
var addCommsExplorerRoute = function (app, options) {
|
|
60
|
+
var _a;
|
|
61
|
+
var routeOpenApi = (options === null || options === void 0 ? void 0 : options.openApi) ? { openApi: options.openApi } : {};
|
|
62
|
+
app.get((_a = options === null || options === void 0 ? void 0 : options.path) !== null && _a !== void 0 ? _a : "/comms/messages", [
|
|
63
|
+
(0, api_1.authenticateMiddleware)(),
|
|
64
|
+
(0, api_1.createOpenApiBuilder)(routeOpenApi)
|
|
65
|
+
.withTags(["admin", "comms"])
|
|
66
|
+
.withSummary("List communication delivery attempts")
|
|
67
|
+
.withQueryParameter("page", { type: "number" }, { required: false })
|
|
68
|
+
.withQueryParameter("limit", { type: "number" }, { required: false })
|
|
69
|
+
.withQueryParameter("channel", { type: "string" }, { required: false })
|
|
70
|
+
.withQueryParameter("status", { type: "string" }, { required: false })
|
|
71
|
+
.withQueryParameter("userId", { type: "string" }, { required: false })
|
|
72
|
+
.withQueryParameter("startDate", { type: "string" }, { required: false })
|
|
73
|
+
.withQueryParameter("endDate", { type: "string" }, { required: false })
|
|
74
|
+
.withResponse(200, {
|
|
75
|
+
data: { items: { type: "object" }, type: "array" },
|
|
76
|
+
limit: { type: "number" },
|
|
77
|
+
more: { type: "boolean" },
|
|
78
|
+
page: { type: "number" },
|
|
79
|
+
total: { type: "number" },
|
|
80
|
+
})
|
|
81
|
+
.build(),
|
|
82
|
+
], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
|
83
|
+
var user, page, limit, skip, match, startTimestamp, endTimestamp, start, end, _a, messages, total;
|
|
84
|
+
return __generator(this, function (_b) {
|
|
85
|
+
switch (_b.label) {
|
|
86
|
+
case 0:
|
|
87
|
+
user = req.user;
|
|
88
|
+
if (!(user === null || user === void 0 ? void 0 : user.admin)) {
|
|
89
|
+
throw new api_1.APIError({ status: 403, title: "Admin access required" });
|
|
90
|
+
}
|
|
91
|
+
page = Math.max(1, Number.parseInt(req.query.page, 10) || 1);
|
|
92
|
+
limit = Math.min(100, Math.max(1, Number.parseInt(req.query.limit, 10) || 20));
|
|
93
|
+
skip = (page - 1) * limit;
|
|
94
|
+
match = {
|
|
95
|
+
deleted: { $ne: true },
|
|
96
|
+
};
|
|
97
|
+
if (req.query.channel) {
|
|
98
|
+
match.channel = req.query.channel;
|
|
99
|
+
}
|
|
100
|
+
if (req.query.status) {
|
|
101
|
+
match.status = req.query.status;
|
|
102
|
+
}
|
|
103
|
+
if (req.query.userId) {
|
|
104
|
+
match.userId = req.query.userId;
|
|
105
|
+
}
|
|
106
|
+
if (req.query.startDate || req.query.endDate) {
|
|
107
|
+
match.created = {};
|
|
108
|
+
}
|
|
109
|
+
if (req.query.startDate && match.created) {
|
|
110
|
+
start = luxon_1.DateTime.fromISO(req.query.startDate, { zone: "utc" });
|
|
111
|
+
if (!start.isValid) {
|
|
112
|
+
throw new api_1.APIError({ status: 400, title: "Invalid startDate format" });
|
|
113
|
+
}
|
|
114
|
+
startTimestamp = start.toMillis();
|
|
115
|
+
match.created.$gte = start.toJSDate();
|
|
116
|
+
}
|
|
117
|
+
if (req.query.endDate && match.created) {
|
|
118
|
+
end = luxon_1.DateTime.fromISO(req.query.endDate, { zone: "utc" });
|
|
119
|
+
if (!end.isValid) {
|
|
120
|
+
throw new api_1.APIError({ status: 400, title: "Invalid endDate format" });
|
|
121
|
+
}
|
|
122
|
+
endTimestamp = end.toMillis();
|
|
123
|
+
match.created.$lte = end.toJSDate();
|
|
124
|
+
}
|
|
125
|
+
if (startTimestamp !== undefined &&
|
|
126
|
+
endTimestamp !== undefined &&
|
|
127
|
+
startTimestamp > endTimestamp) {
|
|
128
|
+
throw new api_1.APIError({ status: 400, title: "startDate must not be after endDate" });
|
|
129
|
+
}
|
|
130
|
+
return [4 /*yield*/, Promise.all([
|
|
131
|
+
commsMessage_1.CommsMessage.find(match).sort("-created").skip(skip).limit(limit),
|
|
132
|
+
commsMessage_1.CommsMessage.countDocuments(match),
|
|
133
|
+
])];
|
|
134
|
+
case 1:
|
|
135
|
+
_a = __read.apply(void 0, [_b.sent(), 2]), messages = _a[0], total = _a[1];
|
|
136
|
+
return [2 /*return*/, res.json({
|
|
137
|
+
data: messages,
|
|
138
|
+
limit: limit,
|
|
139
|
+
more: skip + limit < total,
|
|
140
|
+
page: page,
|
|
141
|
+
total: total,
|
|
142
|
+
})];
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}); }));
|
|
146
|
+
};
|
|
147
|
+
exports.addCommsExplorerRoute = addCommsExplorerRoute;
|