@lenne.tech/nest-server 12.11.3 → 12.12.0
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/.claude/rules/architecture.md +3 -0
- package/.claude/rules/configurable-features.md +6 -0
- package/.claude/rules/package-management.md +6 -4
- package/.claude/rules/testing.md +8 -7
- package/CLAUDE.md +2 -1
- package/FRAMEWORK-API.md +15 -1
- package/dist/core/common/decorators/common-error.decorator.d.ts +1 -1
- package/dist/core/common/interfaces/server-options.interface.d.ts +54 -0
- package/dist/core/common/persistence/audit-log.registry.d.ts +19 -0
- package/dist/core/common/persistence/audit-log.registry.js +84 -0
- package/dist/core/common/persistence/audit-log.registry.js.map +1 -0
- package/dist/core/common/services/core-trust-proxy.initializer.js +1 -1
- package/dist/core/common/services/core-trust-proxy.initializer.js.map +1 -1
- package/dist/core/common/services/crud.service.d.ts +10 -0
- package/dist/core/common/services/crud.service.js +79 -24
- package/dist/core/common/services/crud.service.js.map +1 -1
- package/dist/core/common/services/email.service.d.ts +5 -0
- package/dist/core/common/services/email.service.js +82 -6
- package/dist/core/common/services/email.service.js.map +1 -1
- package/dist/core/common/services/leased-interval.d.ts +22 -0
- package/dist/core/common/services/leased-interval.js +81 -0
- package/dist/core/common/services/leased-interval.js.map +1 -0
- package/dist/core/common/services/mail-outbox.registry.d.ts +26 -0
- package/dist/core/common/services/mail-outbox.registry.js +8 -0
- package/dist/core/common/services/mail-outbox.registry.js.map +1 -0
- package/dist/core/modules/audit-log/audited.decorator.d.ts +2 -0
- package/dist/core/modules/audit-log/audited.decorator.js +10 -0
- package/dist/core/modules/audit-log/audited.decorator.js.map +1 -0
- package/dist/core/modules/audit-log/core-audit-log.constants.d.ts +13 -0
- package/dist/core/modules/audit-log/core-audit-log.constants.js +11 -0
- package/dist/core/modules/audit-log/core-audit-log.constants.js.map +1 -0
- package/dist/core/modules/audit-log/core-audit-log.controller.d.ts +9 -0
- package/dist/core/modules/audit-log/core-audit-log.controller.js +87 -0
- package/dist/core/modules/audit-log/core-audit-log.controller.js.map +1 -0
- package/dist/core/modules/audit-log/core-audit-log.models.d.ts +10 -0
- package/dist/core/modules/audit-log/core-audit-log.models.js +11 -0
- package/dist/core/modules/audit-log/core-audit-log.models.js.map +1 -0
- package/dist/core/modules/audit-log/core-audit-log.module.d.ts +13 -0
- package/dist/core/modules/audit-log/core-audit-log.module.js +41 -0
- package/dist/core/modules/audit-log/core-audit-log.module.js.map +1 -0
- package/dist/core/modules/audit-log/core-audit-log.service.d.ts +49 -0
- package/dist/core/modules/audit-log/core-audit-log.service.js +169 -0
- package/dist/core/modules/audit-log/core-audit-log.service.js.map +1 -0
- package/dist/core/modules/better-auth/core-better-auth-email-verification.service.js +4 -0
- package/dist/core/modules/better-auth/core-better-auth-email-verification.service.js.map +1 -1
- package/dist/core/modules/mail-outbox/core-mail-outbox.constants.d.ts +22 -0
- package/dist/core/modules/mail-outbox/core-mail-outbox.constants.js +23 -0
- package/dist/core/modules/mail-outbox/core-mail-outbox.constants.js.map +1 -0
- package/dist/core/modules/mail-outbox/core-mail-outbox.controller.d.ts +10 -0
- package/dist/core/modules/mail-outbox/core-mail-outbox.controller.js +80 -0
- package/dist/core/modules/mail-outbox/core-mail-outbox.controller.js.map +1 -0
- package/dist/core/modules/mail-outbox/core-mail-outbox.models.d.ts +17 -0
- package/dist/core/modules/mail-outbox/core-mail-outbox.models.js +17 -0
- package/dist/core/modules/mail-outbox/core-mail-outbox.models.js.map +1 -0
- package/dist/core/modules/mail-outbox/core-mail-outbox.module.d.ts +12 -0
- package/dist/core/modules/mail-outbox/core-mail-outbox.module.js +42 -0
- package/dist/core/modules/mail-outbox/core-mail-outbox.module.js.map +1 -0
- package/dist/core/modules/mail-outbox/core-mail-outbox.service.d.ts +59 -0
- package/dist/core/modules/mail-outbox/core-mail-outbox.service.js +280 -0
- package/dist/core/modules/mail-outbox/core-mail-outbox.service.js.map +1 -0
- package/dist/core/modules/migrate/cli/migrate-cli.js +20 -2
- package/dist/core/modules/migrate/cli/migrate-cli.js.map +1 -1
- package/dist/core/modules/throttler/core-throttler.config.d.ts +16 -0
- package/dist/core/modules/throttler/core-throttler.config.js +107 -0
- package/dist/core/modules/throttler/core-throttler.config.js.map +1 -0
- package/dist/core/modules/throttler/core-throttler.constants.d.ts +10 -0
- package/dist/core/modules/throttler/core-throttler.constants.js +11 -0
- package/dist/core/modules/throttler/core-throttler.constants.js.map +1 -0
- package/dist/core/modules/throttler/core-throttler.decorators.d.ts +1 -0
- package/dist/core/modules/throttler/core-throttler.decorators.js +2 -0
- package/dist/core/modules/throttler/core-throttler.decorators.js.map +1 -0
- package/dist/core/modules/throttler/core-throttler.guard.d.ts +15 -0
- package/dist/core/modules/throttler/core-throttler.guard.js +65 -0
- package/dist/core/modules/throttler/core-throttler.guard.js.map +1 -0
- package/dist/core/modules/throttler/core-throttler.models.d.ts +1 -0
- package/dist/core/modules/throttler/core-throttler.models.js +11 -0
- package/dist/core/modules/throttler/core-throttler.models.js.map +1 -0
- package/dist/core/modules/throttler/core-throttler.module.d.ts +12 -0
- package/dist/core/modules/throttler/core-throttler.module.js +71 -0
- package/dist/core/modules/throttler/core-throttler.module.js.map +1 -0
- package/dist/core/modules/throttler/core-throttler.storages.d.ts +43 -0
- package/dist/core/modules/throttler/core-throttler.storages.js +174 -0
- package/dist/core/modules/throttler/core-throttler.storages.js.map +1 -0
- package/dist/core/modules/user/core-user.service.js +2 -2
- package/dist/core/modules/user/core-user.service.js.map +1 -1
- package/dist/core.module.js +26 -0
- package/dist/core.module.js.map +1 -1
- package/dist/index.d.ts +21 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -1
- package/docs/REQUEST-LIFECYCLE.md +7 -0
- package/docs/security-overrides.md +20 -8
- package/migration-guides/12.11.x-to-12.12.x.md +182 -0
- package/package.json +24 -23
- package/src/core/common/interfaces/server-options.interface.ts +311 -1
- package/src/core/common/persistence/audit-log.registry.ts +150 -0
- package/src/core/common/services/core-trust-proxy.initializer.ts +5 -4
- package/src/core/common/services/crud.service.ts +128 -33
- package/src/core/common/services/email.service.ts +101 -9
- package/src/core/common/services/leased-interval.ts +123 -0
- package/src/core/common/services/mail-outbox.registry.ts +53 -0
- package/src/core/modules/audit-log/README.md +95 -0
- package/src/core/modules/audit-log/audited.decorator.ts +27 -0
- package/src/core/modules/audit-log/core-audit-log.constants.ts +28 -0
- package/src/core/modules/audit-log/core-audit-log.controller.ts +77 -0
- package/src/core/modules/audit-log/core-audit-log.models.ts +34 -0
- package/src/core/modules/audit-log/core-audit-log.module.ts +54 -0
- package/src/core/modules/audit-log/core-audit-log.service.ts +270 -0
- package/src/core/modules/better-auth/core-better-auth-email-verification.service.ts +4 -0
- package/src/core/modules/mail-outbox/README.md +88 -0
- package/src/core/modules/mail-outbox/core-mail-outbox.constants.ts +42 -0
- package/src/core/modules/mail-outbox/core-mail-outbox.controller.ts +60 -0
- package/src/core/modules/mail-outbox/core-mail-outbox.models.ts +46 -0
- package/src/core/modules/mail-outbox/core-mail-outbox.module.ts +52 -0
- package/src/core/modules/mail-outbox/core-mail-outbox.service.ts +390 -0
- package/src/core/modules/migrate/README.md +4 -1
- package/src/core/modules/migrate/cli/migrate-cli.ts +30 -2
- package/src/core/modules/throttler/README.md +107 -0
- package/src/core/modules/throttler/core-throttler.config.ts +162 -0
- package/src/core/modules/throttler/core-throttler.constants.ts +33 -0
- package/src/core/modules/throttler/core-throttler.decorators.ts +14 -0
- package/src/core/modules/throttler/core-throttler.guard.ts +109 -0
- package/src/core/modules/throttler/core-throttler.models.ts +23 -0
- package/src/core/modules/throttler/core-throttler.module.ts +95 -0
- package/src/core/modules/throttler/core-throttler.storages.ts +299 -0
- package/src/core/modules/user/core-user.service.ts +3 -2
- package/src/core.module.ts +46 -0
- package/src/index.ts +36 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const MAIL_OUTBOX_MODEL = 'CoreMailOutboxEntry';
|
|
2
|
+
export const MAIL_OUTBOX_COLLECTION = 'mailOutbox';
|
|
3
|
+
export const MAIL_OUTBOX_STATUS = {
|
|
4
|
+
failed: 'failed',
|
|
5
|
+
pending: 'pending',
|
|
6
|
+
sending: 'sending',
|
|
7
|
+
sent: 'sent',
|
|
8
|
+
};
|
|
9
|
+
export const MAIL_OUTBOX_DEFAULTS = {
|
|
10
|
+
failedDays: 30,
|
|
11
|
+
lockSeconds: 900,
|
|
12
|
+
maxAttempts: 10,
|
|
13
|
+
maxRetryDelaySeconds: 3600,
|
|
14
|
+
path: 'mail-outbox',
|
|
15
|
+
pollIntervalSeconds: 10,
|
|
16
|
+
purgeIntervalMs: 60 * 60_000,
|
|
17
|
+
retryDelaySeconds: 30,
|
|
18
|
+
sentDays: 7,
|
|
19
|
+
};
|
|
20
|
+
export const MAIL_OUTBOX_BATCH = 50;
|
|
21
|
+
export const MAIL_OUTBOX_PURGE_BATCH = 1000;
|
|
22
|
+
export const MAIL_OUTBOX_ERROR_LENGTH = 500;
|
|
23
|
+
//# sourceMappingURL=core-mail-outbox.constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-mail-outbox.constants.js","sourceRoot":"","sources":["../../../../src/core/modules/mail-outbox/core-mail-outbox.constants.ts"],"names":[],"mappings":"AAKA,MAAM,CAAC,MAAM,iBAAiB,GAAG,qBAAqB,CAAC;AAGvD,MAAM,CAAC,MAAM,sBAAsB,GAAG,YAAY,CAAC;AAGnD,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAEhC,MAAM,EAAE,QAAQ;IAEhB,OAAO,EAAE,SAAS;IAElB,OAAO,EAAE,SAAS;IAElB,IAAI,EAAE,MAAM;CACJ,CAAC;AAGX,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,UAAU,EAAE,EAAE;IACd,WAAW,EAAE,GAAG;IAChB,WAAW,EAAE,EAAE;IACf,oBAAoB,EAAE,IAAI;IAC1B,IAAI,EAAE,aAAa;IACnB,mBAAmB,EAAE,EAAE;IAEvB,eAAe,EAAE,EAAE,GAAG,MAAM;IAC5B,iBAAiB,EAAE,EAAE;IACrB,QAAQ,EAAE,CAAC;CACZ,CAAC;AAGF,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AACpC,MAAM,CAAC,MAAM,uBAAuB,GAAG,IAAI,CAAC;AAG5C,MAAM,CAAC,MAAM,wBAAwB,GAAG,GAAG,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CoreMailOutboxService } from './core-mail-outbox.service.js';
|
|
2
|
+
export declare class CoreMailOutboxController {
|
|
3
|
+
protected readonly mailOutboxService: CoreMailOutboxService;
|
|
4
|
+
constructor(mailOutboxService: CoreMailOutboxService);
|
|
5
|
+
list(status?: string, limit?: string, offset?: string): Promise<{
|
|
6
|
+
items: import("./core-mail-outbox.service.js").MailOutboxOverview[];
|
|
7
|
+
total: number;
|
|
8
|
+
}>;
|
|
9
|
+
retry(id: string): Promise<import("./core-mail-outbox.service.js").MailOutboxOverview>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
+
};
|
|
13
|
+
import { BadRequestException, Controller, Get, NotFoundException, Param, Post, Query } from '@nestjs/common';
|
|
14
|
+
import { ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
|
|
15
|
+
import { Roles } from '../../common/decorators/roles.decorator.js';
|
|
16
|
+
import { RoleEnum } from '../../common/enums/role.enum.js';
|
|
17
|
+
import { SkipTenantCheck } from '../tenant/core-tenant.decorators.js';
|
|
18
|
+
import { CoreMailOutboxService } from './core-mail-outbox.service.js';
|
|
19
|
+
function parseCount(value, name) {
|
|
20
|
+
if (value === undefined || value === '') {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
if (!/^\d+$/.test(value)) {
|
|
24
|
+
throw new BadRequestException(`${name} must be a non-negative integer`);
|
|
25
|
+
}
|
|
26
|
+
return Number(value);
|
|
27
|
+
}
|
|
28
|
+
let CoreMailOutboxController = class CoreMailOutboxController {
|
|
29
|
+
mailOutboxService;
|
|
30
|
+
constructor(mailOutboxService) {
|
|
31
|
+
this.mailOutboxService = mailOutboxService;
|
|
32
|
+
}
|
|
33
|
+
async list(status, limit, offset) {
|
|
34
|
+
if (status !== undefined && status !== '' && !['failed', 'pending', 'sending', 'sent'].includes(status)) {
|
|
35
|
+
throw new BadRequestException('status must be one of pending, sending, sent, failed');
|
|
36
|
+
}
|
|
37
|
+
return this.mailOutboxService.list({
|
|
38
|
+
limit: parseCount(limit, 'limit'),
|
|
39
|
+
offset: parseCount(offset, 'offset'),
|
|
40
|
+
status: (status || undefined),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async retry(id) {
|
|
44
|
+
const entry = await this.mailOutboxService.retry(id);
|
|
45
|
+
if (!entry) {
|
|
46
|
+
throw new NotFoundException('No failed mail with this id');
|
|
47
|
+
}
|
|
48
|
+
return entry;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
__decorate([
|
|
52
|
+
ApiOperation({ summary: 'Stored mails without their content, newest first' }),
|
|
53
|
+
ApiQuery({ enum: ['pending', 'sending', 'sent', 'failed'], name: 'status', required: false }),
|
|
54
|
+
ApiQuery({ name: 'limit', required: false }),
|
|
55
|
+
ApiQuery({ name: 'offset', required: false }),
|
|
56
|
+
Get(),
|
|
57
|
+
__param(0, Query('status')),
|
|
58
|
+
__param(1, Query('limit')),
|
|
59
|
+
__param(2, Query('offset')),
|
|
60
|
+
__metadata("design:type", Function),
|
|
61
|
+
__metadata("design:paramtypes", [String, String, String]),
|
|
62
|
+
__metadata("design:returntype", Promise)
|
|
63
|
+
], CoreMailOutboxController.prototype, "list", null);
|
|
64
|
+
__decorate([
|
|
65
|
+
ApiOperation({ summary: 'Give a failed mail a new round of attempts' }),
|
|
66
|
+
Post(':id/retry'),
|
|
67
|
+
__param(0, Param('id')),
|
|
68
|
+
__metadata("design:type", Function),
|
|
69
|
+
__metadata("design:paramtypes", [String]),
|
|
70
|
+
__metadata("design:returntype", Promise)
|
|
71
|
+
], CoreMailOutboxController.prototype, "retry", null);
|
|
72
|
+
CoreMailOutboxController = __decorate([
|
|
73
|
+
ApiTags('mail-outbox'),
|
|
74
|
+
Controller('mail-outbox'),
|
|
75
|
+
Roles(RoleEnum.ADMIN),
|
|
76
|
+
SkipTenantCheck(),
|
|
77
|
+
__metadata("design:paramtypes", [CoreMailOutboxService])
|
|
78
|
+
], CoreMailOutboxController);
|
|
79
|
+
export { CoreMailOutboxController };
|
|
80
|
+
//# sourceMappingURL=core-mail-outbox.controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-mail-outbox.controller.js","sourceRoot":"","sources":["../../../../src/core/modules/mail-outbox/core-mail-outbox.controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAE,GAAG,EAAE,iBAAiB,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAC7G,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,OAAO,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
|
2
|
+
export declare const MailOutboxEntrySchema: any;
|
|
3
|
+
export interface CoreMailOutboxEntry {
|
|
4
|
+
attempts: number;
|
|
5
|
+
id: string;
|
|
6
|
+
lastAttemptAt?: Date;
|
|
7
|
+
lastError?: string;
|
|
8
|
+
lockedBy?: string;
|
|
9
|
+
lockedUntil?: Date;
|
|
10
|
+
mail?: QueuedSmtpMail | null;
|
|
11
|
+
nextAttemptAt?: Date;
|
|
12
|
+
queuedAt: Date;
|
|
13
|
+
sentAt?: Date;
|
|
14
|
+
status: 'failed' | 'pending' | 'sending' | 'sent';
|
|
15
|
+
subject: string;
|
|
16
|
+
to: string[];
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createSchema } from '../../common/persistence/mongo/mongoose-compat.js';
|
|
2
|
+
import { MAIL_OUTBOX_COLLECTION } from './core-mail-outbox.constants.js';
|
|
3
|
+
export const MailOutboxEntrySchema = createSchema({
|
|
4
|
+
attempts: { default: 0, type: Number },
|
|
5
|
+
lastAttemptAt: { type: Date },
|
|
6
|
+
lastError: { type: String },
|
|
7
|
+
lockedBy: { type: String },
|
|
8
|
+
lockedUntil: { type: Date },
|
|
9
|
+
mail: { type: Object },
|
|
10
|
+
nextAttemptAt: { index: true, type: Date },
|
|
11
|
+
queuedAt: { index: true, type: Date },
|
|
12
|
+
sentAt: { index: true, type: Date },
|
|
13
|
+
status: { index: true, type: String },
|
|
14
|
+
subject: { type: String },
|
|
15
|
+
to: { default: [], type: [String] },
|
|
16
|
+
}, { collection: MAIL_OUTBOX_COLLECTION, versionKey: false });
|
|
17
|
+
//# sourceMappingURL=core-mail-outbox.models.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-mail-outbox.models.js","sourceRoot":"","sources":["../../../../src/core/modules/mail-outbox/core-mail-outbox.models.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mDAAmD,CAAC;AAEjF,OAAO,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AAUzE,MAAM,CAAC,MAAM,qBAAqB,GAAG,YAAY,CAC/C;IACE,QAAQ,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE;IACtC,aAAa,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE;IAC7B,SAAS,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;IAC3B,QAAQ,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;IAC1B,WAAW,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE;IAC3B,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;IACtB,aAAa,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE;IAC1C,QAAQ,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE;IACrC,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE;IACnC,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE;IACrC,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;IACzB,EAAE,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,MAAM,CAAC,EAAE;CACpC,EACD,EAAE,UAAU,EAAE,sBAAsB,EAAE,UAAU,EAAE,KAAK,EAAE,CAC1D,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DynamicModule, Type } from '@nestjs/common';
|
|
2
|
+
import { CoreMailOutboxController } from './core-mail-outbox.controller.js';
|
|
3
|
+
import { CoreMailOutboxService } from './core-mail-outbox.service.js';
|
|
4
|
+
export interface CoreMailOutboxModuleOptions {
|
|
5
|
+
controller?: Type<CoreMailOutboxController>;
|
|
6
|
+
path?: string;
|
|
7
|
+
roles?: unknown;
|
|
8
|
+
service?: Type<CoreMailOutboxService>;
|
|
9
|
+
}
|
|
10
|
+
export declare class CoreMailOutboxModule {
|
|
11
|
+
static forRoot(options?: CoreMailOutboxModuleOptions): DynamicModule;
|
|
12
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var CoreMailOutboxModule_1;
|
|
8
|
+
import { Module } from '@nestjs/common';
|
|
9
|
+
import { PATH_METADATA } from '@nestjs/common/constants';
|
|
10
|
+
import { RoleEnum } from '../../common/enums/role.enum.js';
|
|
11
|
+
import { CorePersistenceModule } from '../../common/persistence/core-persistence.module.js';
|
|
12
|
+
import { MAIL_OUTBOX_DEFAULTS, MAIL_OUTBOX_MODEL } from './core-mail-outbox.constants.js';
|
|
13
|
+
import { CoreMailOutboxController } from './core-mail-outbox.controller.js';
|
|
14
|
+
import { MailOutboxEntrySchema } from './core-mail-outbox.models.js';
|
|
15
|
+
import { CoreMailOutboxService } from './core-mail-outbox.service.js';
|
|
16
|
+
let CoreMailOutboxModule = CoreMailOutboxModule_1 = class CoreMailOutboxModule {
|
|
17
|
+
static forRoot(options = {}) {
|
|
18
|
+
const controller = options.controller ?? CoreMailOutboxController;
|
|
19
|
+
Reflect.defineMetadata(PATH_METADATA, options.path || MAIL_OUTBOX_DEFAULTS.path, controller);
|
|
20
|
+
if (controller === CoreMailOutboxController || !Reflect.hasOwnMetadata('roles', controller)) {
|
|
21
|
+
const extra = Array.isArray(options.roles)
|
|
22
|
+
? options.roles.filter((role) => typeof role === 'string' && !!role)
|
|
23
|
+
: [];
|
|
24
|
+
Reflect.defineMetadata('roles', [...new Set([RoleEnum.ADMIN, ...extra])], controller);
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
controllers: [controller],
|
|
28
|
+
exports: [CoreMailOutboxService],
|
|
29
|
+
global: true,
|
|
30
|
+
imports: [CorePersistenceModule.forFeature([{ name: MAIL_OUTBOX_MODEL, schema: MailOutboxEntrySchema }])],
|
|
31
|
+
module: CoreMailOutboxModule_1,
|
|
32
|
+
providers: [
|
|
33
|
+
options.service ? { provide: CoreMailOutboxService, useClass: options.service } : CoreMailOutboxService,
|
|
34
|
+
],
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
CoreMailOutboxModule = CoreMailOutboxModule_1 = __decorate([
|
|
39
|
+
Module({})
|
|
40
|
+
], CoreMailOutboxModule);
|
|
41
|
+
export { CoreMailOutboxModule };
|
|
42
|
+
//# sourceMappingURL=core-mail-outbox.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-mail-outbox.module.js","sourceRoot":"","sources":["../../../../src/core/modules/mail-outbox/core-mail-outbox.module.ts"],"names":[],"mappings":";;;;;;;AAAA,OAAO,EAAiB,MAAM,EAAQ,MAAM,gBAAgB,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD,OAAO,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAC;AAC3D,OAAO,EAAE,qBAAqB,EAAE,MAAM,qDAAqD,CAAC;AAC5F,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AAC1F,OAAO,EAAE,wBAAwB,EAAE,MAAM,kCAAkC,CAAC;AAC5E,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAqB/D,IAAM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AAA;AAtBY,oBAAoB;IADhC,MAAM,CAAC,EAAE,CAAC;GACE,oBAAoB,CAsBhC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Logger, OnApplicationBootstrap, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import type { IMailOutbox } from '../../common/interfaces/server-options.interface.js';
|
|
3
|
+
import type { DataStore, Where } from '../../common/persistence/data-store.interface.js';
|
|
4
|
+
import { CoreRedisService } from '../../common/services/core-redis.service.js';
|
|
5
|
+
import { EmailService } from '../../common/services/email.service.js';
|
|
6
|
+
import { LeasedInterval } from '../../common/services/leased-interval.js';
|
|
7
|
+
import { type MailOutbox, type QueuedMailReceipt, type QueuedSmtpMail } from '../../common/services/mail-outbox.registry.js';
|
|
8
|
+
import type { CoreMailOutboxEntry } from './core-mail-outbox.models.js';
|
|
9
|
+
export interface ResolvedMailOutboxSettings {
|
|
10
|
+
failedDays: false | number;
|
|
11
|
+
lockSeconds: number;
|
|
12
|
+
maxAttempts: number;
|
|
13
|
+
maxRetryDelaySeconds: number;
|
|
14
|
+
pollIntervalSeconds: number;
|
|
15
|
+
retryDelaySeconds: number;
|
|
16
|
+
sentDays: false | number;
|
|
17
|
+
}
|
|
18
|
+
export declare function resolveMailOutboxSettings(config?: IMailOutbox): ResolvedMailOutboxSettings;
|
|
19
|
+
export interface MailOutboxQuery {
|
|
20
|
+
limit?: number;
|
|
21
|
+
offset?: number;
|
|
22
|
+
status?: CoreMailOutboxEntry['status'];
|
|
23
|
+
}
|
|
24
|
+
export type MailOutboxOverview = Omit<CoreMailOutboxEntry, 'mail'>;
|
|
25
|
+
export declare class CoreMailOutboxService implements MailOutbox, OnApplicationBootstrap, OnModuleDestroy, OnModuleInit {
|
|
26
|
+
protected readonly store: DataStore<CoreMailOutboxEntry>;
|
|
27
|
+
protected readonly emailService: EmailService;
|
|
28
|
+
protected readonly redisService?: CoreRedisService;
|
|
29
|
+
protected readonly logger: Logger;
|
|
30
|
+
protected readonly owner: string;
|
|
31
|
+
protected readonly settings: ResolvedMailOutboxSettings;
|
|
32
|
+
protected delivering?: Promise<number>;
|
|
33
|
+
protected deliverAgain: boolean;
|
|
34
|
+
protected pollTimer?: ReturnType<typeof setInterval>;
|
|
35
|
+
protected purge?: LeasedInterval;
|
|
36
|
+
protected stopped: boolean;
|
|
37
|
+
protected purgeIntervalMs: number;
|
|
38
|
+
constructor(store: DataStore<CoreMailOutboxEntry>, emailService: EmailService, redisService?: CoreRedisService);
|
|
39
|
+
protected get config(): IMailOutbox;
|
|
40
|
+
onModuleInit(): void;
|
|
41
|
+
onApplicationBootstrap(): void;
|
|
42
|
+
onModuleDestroy(): Promise<void>;
|
|
43
|
+
enqueue(mail: QueuedSmtpMail): Promise<QueuedMailReceipt>;
|
|
44
|
+
protected messageIdOf(id: string, from: string): string;
|
|
45
|
+
kick(): void;
|
|
46
|
+
whenIdle(): Promise<void>;
|
|
47
|
+
deliverDue(): Promise<number>;
|
|
48
|
+
protected claimNext(): Promise<CoreMailOutboxEntry | null>;
|
|
49
|
+
protected deliver(entry: CoreMailOutboxEntry): Promise<void>;
|
|
50
|
+
protected recordFailure(entry: CoreMailOutboxEntry, error: unknown): Promise<void>;
|
|
51
|
+
protected retryDelayMs(attempts: number): number;
|
|
52
|
+
list(query?: MailOutboxQuery): Promise<{
|
|
53
|
+
items: MailOutboxOverview[];
|
|
54
|
+
total: number;
|
|
55
|
+
}>;
|
|
56
|
+
retry(id: string): Promise<MailOutboxOverview | null>;
|
|
57
|
+
purgeExpired(now?: Date): Promise<number>;
|
|
58
|
+
protected purgeWhere(where: Where<CoreMailOutboxEntry>): Promise<number>;
|
|
59
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
+
};
|
|
13
|
+
var CoreMailOutboxService_1;
|
|
14
|
+
import { Injectable, Logger, Optional } from '@nestjs/common';
|
|
15
|
+
import { maskEmail } from '../../common/helpers/logging.helper.js';
|
|
16
|
+
import { InjectStore } from '../../common/persistence/inject-store.decorator.js';
|
|
17
|
+
import { ConfigService } from '../../common/services/config.service.js';
|
|
18
|
+
import { CoreRedisService } from '../../common/services/core-redis.service.js';
|
|
19
|
+
import { EmailService } from '../../common/services/email.service.js';
|
|
20
|
+
import { frameworkJobLeaseStore, LeasedInterval } from '../../common/services/leased-interval.js';
|
|
21
|
+
import { getMailOutbox, setMailOutbox, } from '../../common/services/mail-outbox.registry.js';
|
|
22
|
+
import { MAIL_OUTBOX_BATCH, MAIL_OUTBOX_DEFAULTS, MAIL_OUTBOX_ERROR_LENGTH, MAIL_OUTBOX_MODEL, MAIL_OUTBOX_PURGE_BATCH, MAIL_OUTBOX_STATUS, } from './core-mail-outbox.constants.js';
|
|
23
|
+
function positiveOr(value, fallback) {
|
|
24
|
+
return typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : fallback;
|
|
25
|
+
}
|
|
26
|
+
function daysOr(value, fallback) {
|
|
27
|
+
return value === false ? false : positiveOr(value, fallback);
|
|
28
|
+
}
|
|
29
|
+
export function resolveMailOutboxSettings(config = {}) {
|
|
30
|
+
return {
|
|
31
|
+
failedDays: daysOr(config.retention?.failedDays, MAIL_OUTBOX_DEFAULTS.failedDays),
|
|
32
|
+
lockSeconds: positiveOr(config.lockSeconds, MAIL_OUTBOX_DEFAULTS.lockSeconds),
|
|
33
|
+
maxAttempts: Math.floor(positiveOr(config.maxAttempts, MAIL_OUTBOX_DEFAULTS.maxAttempts)) || 1,
|
|
34
|
+
maxRetryDelaySeconds: positiveOr(config.maxRetryDelaySeconds, MAIL_OUTBOX_DEFAULTS.maxRetryDelaySeconds),
|
|
35
|
+
pollIntervalSeconds: positiveOr(config.pollIntervalSeconds, MAIL_OUTBOX_DEFAULTS.pollIntervalSeconds),
|
|
36
|
+
retryDelaySeconds: positiveOr(config.retryDelaySeconds, MAIL_OUTBOX_DEFAULTS.retryDelaySeconds),
|
|
37
|
+
sentDays: daysOr(config.retention?.sentDays, MAIL_OUTBOX_DEFAULTS.sentDays),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
let CoreMailOutboxService = CoreMailOutboxService_1 = class CoreMailOutboxService {
|
|
41
|
+
store;
|
|
42
|
+
emailService;
|
|
43
|
+
redisService;
|
|
44
|
+
logger = new Logger(CoreMailOutboxService_1.name);
|
|
45
|
+
owner = `${process.pid}:${Math.random().toString(36).slice(2)}`;
|
|
46
|
+
settings;
|
|
47
|
+
delivering;
|
|
48
|
+
deliverAgain = false;
|
|
49
|
+
pollTimer;
|
|
50
|
+
purge;
|
|
51
|
+
stopped = false;
|
|
52
|
+
purgeIntervalMs = MAIL_OUTBOX_DEFAULTS.purgeIntervalMs;
|
|
53
|
+
constructor(store, emailService, redisService) {
|
|
54
|
+
this.store = store;
|
|
55
|
+
this.emailService = emailService;
|
|
56
|
+
this.redisService = redisService;
|
|
57
|
+
this.settings = resolveMailOutboxSettings(this.config);
|
|
58
|
+
}
|
|
59
|
+
get config() {
|
|
60
|
+
const value = ConfigService.configFastButReadOnly?.mailOutbox;
|
|
61
|
+
return value && typeof value === 'object' ? value : {};
|
|
62
|
+
}
|
|
63
|
+
onModuleInit() {
|
|
64
|
+
setMailOutbox(this);
|
|
65
|
+
}
|
|
66
|
+
onApplicationBootstrap() {
|
|
67
|
+
this.logger.log(`Mail outbox active: up to ${this.settings.maxAttempts} attempts, retries from ${this.settings.retryDelaySeconds} s`);
|
|
68
|
+
this.pollTimer = setInterval(() => this.kick(), this.settings.pollIntervalSeconds * 1000);
|
|
69
|
+
this.pollTimer.unref?.();
|
|
70
|
+
this.kick();
|
|
71
|
+
if (this.settings.sentDays !== false || this.settings.failedDays !== false) {
|
|
72
|
+
this.purge = new LeasedInterval({
|
|
73
|
+
intervalMs: this.purgeIntervalMs,
|
|
74
|
+
lease: () => frameworkJobLeaseStore(this.redisService, 'mail-outbox'),
|
|
75
|
+
name: 'MailOutboxPurge',
|
|
76
|
+
run: () => this.purgeExpired(),
|
|
77
|
+
});
|
|
78
|
+
this.purge.start();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async onModuleDestroy() {
|
|
82
|
+
this.stopped = true;
|
|
83
|
+
if (getMailOutbox() === this) {
|
|
84
|
+
setMailOutbox(undefined);
|
|
85
|
+
}
|
|
86
|
+
if (this.pollTimer) {
|
|
87
|
+
clearInterval(this.pollTimer);
|
|
88
|
+
this.pollTimer = undefined;
|
|
89
|
+
}
|
|
90
|
+
await this.purge?.stop();
|
|
91
|
+
await this.delivering?.catch(() => undefined);
|
|
92
|
+
}
|
|
93
|
+
async enqueue(mail) {
|
|
94
|
+
const now = new Date();
|
|
95
|
+
const entry = await this.store.insert({
|
|
96
|
+
attempts: 0,
|
|
97
|
+
mail,
|
|
98
|
+
nextAttemptAt: now,
|
|
99
|
+
queuedAt: now,
|
|
100
|
+
status: MAIL_OUTBOX_STATUS.pending,
|
|
101
|
+
subject: mail.subject,
|
|
102
|
+
to: mail.to,
|
|
103
|
+
});
|
|
104
|
+
this.kick();
|
|
105
|
+
return { id: entry.id, messageId: this.messageIdOf(entry.id, mail.from) };
|
|
106
|
+
}
|
|
107
|
+
messageIdOf(id, from) {
|
|
108
|
+
const domain = /@([^>\s]+)>?\s*$/.exec(from ?? '')?.[1] || 'localhost';
|
|
109
|
+
return `<${id}.outbox@${domain}>`;
|
|
110
|
+
}
|
|
111
|
+
kick() {
|
|
112
|
+
if (this.stopped) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (this.delivering) {
|
|
116
|
+
this.deliverAgain = true;
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
this.delivering = this.deliverDue()
|
|
120
|
+
.then((attempted) => {
|
|
121
|
+
if (attempted >= MAIL_OUTBOX_BATCH) {
|
|
122
|
+
this.deliverAgain = true;
|
|
123
|
+
}
|
|
124
|
+
return attempted;
|
|
125
|
+
})
|
|
126
|
+
.catch((error) => {
|
|
127
|
+
this.logger.warn(`Mail outbox delivery failed, trying again later: ${error?.message ?? error}`);
|
|
128
|
+
return 0;
|
|
129
|
+
})
|
|
130
|
+
.finally(() => {
|
|
131
|
+
this.delivering = undefined;
|
|
132
|
+
if (this.deliverAgain) {
|
|
133
|
+
this.deliverAgain = false;
|
|
134
|
+
this.kick();
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
async whenIdle() {
|
|
139
|
+
while (this.delivering) {
|
|
140
|
+
await this.delivering.catch(() => undefined);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async deliverDue() {
|
|
144
|
+
let attempted = 0;
|
|
145
|
+
while (attempted < MAIL_OUTBOX_BATCH && !this.stopped) {
|
|
146
|
+
const entry = await this.claimNext();
|
|
147
|
+
if (!entry) {
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
attempted++;
|
|
151
|
+
await this.deliver(entry);
|
|
152
|
+
}
|
|
153
|
+
return attempted;
|
|
154
|
+
}
|
|
155
|
+
async claimNext() {
|
|
156
|
+
const now = new Date();
|
|
157
|
+
return (await this.store.updateOne({
|
|
158
|
+
$or: [
|
|
159
|
+
{ nextAttemptAt: { $lte: now }, status: MAIL_OUTBOX_STATUS.pending },
|
|
160
|
+
{ lockedUntil: { $lte: now }, status: MAIL_OUTBOX_STATUS.sending },
|
|
161
|
+
],
|
|
162
|
+
}, {
|
|
163
|
+
$inc: { attempts: 1 },
|
|
164
|
+
$set: {
|
|
165
|
+
lastAttemptAt: now,
|
|
166
|
+
lockedBy: this.owner,
|
|
167
|
+
lockedUntil: new Date(now.getTime() + this.settings.lockSeconds * 1000),
|
|
168
|
+
status: MAIL_OUTBOX_STATUS.sending,
|
|
169
|
+
},
|
|
170
|
+
}));
|
|
171
|
+
}
|
|
172
|
+
async deliver(entry) {
|
|
173
|
+
try {
|
|
174
|
+
if (!entry.mail) {
|
|
175
|
+
throw new Error('the stored mail has no content');
|
|
176
|
+
}
|
|
177
|
+
await this.emailService.deliverQueuedMail(entry.mail, this.messageIdOf(entry.id, entry.mail.from));
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
await this.recordFailure(entry, error);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const recorded = await this.store.updateOne({ id: entry.id, lockedBy: this.owner, status: MAIL_OUTBOX_STATUS.sending }, {
|
|
184
|
+
$set: { lastError: '', mail: null, sentAt: new Date(), status: MAIL_OUTBOX_STATUS.sent },
|
|
185
|
+
$unset: { lockedBy: '', lockedUntil: '' },
|
|
186
|
+
});
|
|
187
|
+
if (!recorded) {
|
|
188
|
+
this.logger.warn(`Mail "${entry.subject}" was delivered after its claim expired — another replica may deliver it again ` +
|
|
189
|
+
'(raise mailOutbox.lockSeconds above the SMTP timeouts)');
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async recordFailure(entry, error) {
|
|
193
|
+
const message = String(error?.message ?? error).slice(0, MAIL_OUTBOX_ERROR_LENGTH);
|
|
194
|
+
const recipients = (entry.to ?? []).map(maskEmail).join(', ');
|
|
195
|
+
const final = entry.attempts >= this.settings.maxAttempts;
|
|
196
|
+
const nextAttemptAt = new Date(Date.now() + this.retryDelayMs(entry.attempts));
|
|
197
|
+
await this.store.updateOne({ id: entry.id, lockedBy: this.owner }, {
|
|
198
|
+
$set: final
|
|
199
|
+
? { lastError: message, status: MAIL_OUTBOX_STATUS.failed }
|
|
200
|
+
: { lastError: message, nextAttemptAt, status: MAIL_OUTBOX_STATUS.pending },
|
|
201
|
+
$unset: { lockedBy: '', lockedUntil: '' },
|
|
202
|
+
});
|
|
203
|
+
if (final) {
|
|
204
|
+
this.logger.error(`Mail "${entry.subject}" to ${recipients} failed for good after ${entry.attempts} attempt(s): ${message}`);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
this.logger.warn(`Mail "${entry.subject}" to ${recipients}: attempt ${entry.attempts} of ${this.settings.maxAttempts} failed ` +
|
|
208
|
+
`(${message}), next at ${nextAttemptAt.toISOString()}`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
retryDelayMs(attempts) {
|
|
212
|
+
const seconds = this.settings.retryDelaySeconds * 2 ** Math.max(0, attempts - 1);
|
|
213
|
+
return Math.min(seconds, this.settings.maxRetryDelaySeconds) * 1000;
|
|
214
|
+
}
|
|
215
|
+
async list(query = {}) {
|
|
216
|
+
const where = query.status && Object.values(MAIL_OUTBOX_STATUS).includes(query.status)
|
|
217
|
+
? { status: query.status }
|
|
218
|
+
: {};
|
|
219
|
+
const limit = Math.min(500, Number.isInteger(query.limit) && query.limit > 0 ? query.limit : 50);
|
|
220
|
+
const offset = Number.isInteger(query.offset) && query.offset > 0 ? query.offset : 0;
|
|
221
|
+
const [items, total] = await Promise.all([
|
|
222
|
+
this.store.find(where, { exclude: ['mail'], limit, skip: offset, sort: { queuedAt: -1 } }),
|
|
223
|
+
this.store.count(where),
|
|
224
|
+
]);
|
|
225
|
+
return { items: items, total };
|
|
226
|
+
}
|
|
227
|
+
async retry(id) {
|
|
228
|
+
const entry = await this.store.updateOne({ id, status: MAIL_OUTBOX_STATUS.failed }, {
|
|
229
|
+
$set: { attempts: 0, nextAttemptAt: new Date(), status: MAIL_OUTBOX_STATUS.pending },
|
|
230
|
+
});
|
|
231
|
+
if (!entry) {
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
this.kick();
|
|
235
|
+
const { mail: _content, ...overview } = entry;
|
|
236
|
+
return overview;
|
|
237
|
+
}
|
|
238
|
+
async purgeExpired(now = new Date()) {
|
|
239
|
+
let removed = 0;
|
|
240
|
+
const { failedDays, sentDays } = this.settings;
|
|
241
|
+
if (sentDays !== false) {
|
|
242
|
+
removed += await this.purgeWhere({
|
|
243
|
+
sentAt: { $lt: new Date(now.getTime() - sentDays * 86_400_000) },
|
|
244
|
+
status: MAIL_OUTBOX_STATUS.sent,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
if (failedDays !== false) {
|
|
248
|
+
removed += await this.purgeWhere({
|
|
249
|
+
lastAttemptAt: { $lt: new Date(now.getTime() - failedDays * 86_400_000) },
|
|
250
|
+
status: MAIL_OUTBOX_STATUS.failed,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
if (removed) {
|
|
254
|
+
this.logger.log(`Mail outbox: ${removed} entr${removed === 1 ? 'y' : 'ies'} past the retention removed`);
|
|
255
|
+
}
|
|
256
|
+
return removed;
|
|
257
|
+
}
|
|
258
|
+
async purgeWhere(where) {
|
|
259
|
+
let removed = 0;
|
|
260
|
+
for (;;) {
|
|
261
|
+
const batch = await this.store.find(where, { limit: MAIL_OUTBOX_PURGE_BATCH, select: ['id'] });
|
|
262
|
+
if (!batch.length) {
|
|
263
|
+
return removed;
|
|
264
|
+
}
|
|
265
|
+
removed += await this.store.deleteMany({ ...where, id: { $in: batch.map((entry) => entry.id) } });
|
|
266
|
+
if (batch.length < MAIL_OUTBOX_PURGE_BATCH) {
|
|
267
|
+
return removed;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
CoreMailOutboxService = CoreMailOutboxService_1 = __decorate([
|
|
273
|
+
Injectable(),
|
|
274
|
+
__param(0, InjectStore(MAIL_OUTBOX_MODEL)),
|
|
275
|
+
__param(2, Optional()),
|
|
276
|
+
__metadata("design:paramtypes", [Object, EmailService,
|
|
277
|
+
CoreRedisService])
|
|
278
|
+
], CoreMailOutboxService);
|
|
279
|
+
export { CoreMailOutboxService };
|
|
280
|
+
//# sourceMappingURL=core-mail-outbox.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-mail-outbox.service.js","sourceRoot":"","sources":["../../../../src/core/modules/mail-outbox/core-mail-outbox.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAyD,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAErH,OAAO,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAC;AAGnE,OAAO,EAAE,WAAW,EAAE,MAAM,oDAAoD,CAAC;AACjF,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AACxE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6CAA6C,CAAC;AAC/E,OAAO,EAAE,YAAY,EAAE,MAAM,wCAAwC,CAAC;AACtE,OAAO,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,0CAA0C,CAAC;AAClG,OAAO,EACL,aAAa,EAIb,aAAa,GACd,MAAM,+CAA+C,CAAC;AACvD,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,wBAAwB,EACxB,iBAAiB,EACjB,uBAAuB,EACvB,kBAAkB,GACnB,MAAM,iCAAiC,CAAC;AAczC,SAAS,UAAU,CAAC,KAAc,EAAE,QAAgB;IAClD,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC;AAC7F,CAAC;AAED,SAAS,MAAM,CAAC,KAAc,EAAE,QAAgB;IAC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OAAO,EAAE,CAAC;YACZ,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,gBAAgB,OAAO,QAAQ,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,6BAA6B,CAAC,CAAC;QAC3G,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAES,KAAK,CAAC,UAAU,CAAC,KAAiC;QAC1D,IAAI,OAAO,GAAG,CAAC,CAAC;QAChB,SAAS,CAAC;YACR,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,uBAAuB,EAAE,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YAC/F,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;gBAClB,OAAO,OAAO,CAAC;YACjB,CAAC;YACD,OAAO,IAAI,MAAM,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE,GAAG,KAAK,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;YAClG,IAAI,KAAK,CAAC,MAAM,GAAG,uBAAuB,EAAE,CAAC;gBAC3C,OAAO,OAAO,CAAC;YACjB,CAAC;QACH,CAAC;IACH,CAAC;CACF,CAAA;AA1SY,qBAAqB;IADjC,UAAU,EAAE;IAgBR,WAAA,WAAW,CAAC,iBAAiB,CAAC,CAAA;IAE9B,WAAA,QAAQ,EAAE,CAAA;6CADsB,YAAY;QACC,gBAAgB;GAjBrD,qBAAqB,CA0SjC"}
|
|
@@ -18,8 +18,26 @@ export function vendoredFrameworkSpecifier(migrationsDir, cwd = process.cwd(), c
|
|
|
18
18
|
if (!['index.ts', 'index.js'].some((entry) => fs.existsSync(path.join(coreRoot, entry)))) {
|
|
19
19
|
return undefined;
|
|
20
20
|
}
|
|
21
|
-
const
|
|
22
|
-
|
|
21
|
+
const absoluteMigrationsDir = path.resolve(cwd, migrationsDir);
|
|
22
|
+
const relativePath = path.relative(absoluteMigrationsDir, coreRoot).split(path.sep).join('/');
|
|
23
|
+
const directory = relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
|
|
24
|
+
return isEsModuleScope(absoluteMigrationsDir) ? `${directory}/index.js` : directory;
|
|
25
|
+
}
|
|
26
|
+
function isEsModuleScope(directory) {
|
|
27
|
+
for (let dir = directory;; dir = path.dirname(dir)) {
|
|
28
|
+
const manifest = path.join(dir, 'package.json');
|
|
29
|
+
if (fs.existsSync(manifest)) {
|
|
30
|
+
try {
|
|
31
|
+
return JSON.parse(fs.readFileSync(manifest, 'utf-8'))?.type === 'module';
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (path.dirname(dir) === dir) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
23
41
|
}
|
|
24
42
|
export function rewriteFrameworkSpecifier(template, specifier) {
|
|
25
43
|
return template.replace(/(['"])@lenne\.tech\/nest-server\1/g, (_match, quote) => `${quote}${specifier}${quote}`);
|