@maioradv/nestjs-core 2.2.2 → 2.2.4

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.
@@ -1,7 +1,9 @@
1
1
  import { AccountsApiConfigs } from "@maioradv/accounts-lib";
2
+ import { NotificationsApiConfigs } from "@maioradv/notifications-lib";
2
3
  export type MaiorConfig = {
3
4
  accounts?: AccountsApiConfigs;
4
5
  smtp?: SmtpConfig;
6
+ notifications?: NotificationsApiConfigs;
5
7
  };
6
8
  export type SmtpConfig = {
7
9
  host: string;
@@ -1,5 +1,6 @@
1
1
  export * from './crypt.service';
2
2
  export * from './s3.service';
3
3
  export * from './accounts.service';
4
+ export * from './notifications.service';
4
5
  export * from './storage.service';
5
6
  export * from './mailer.service';
@@ -17,5 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./crypt.service"), exports);
18
18
  __exportStar(require("./s3.service"), exports);
19
19
  __exportStar(require("./accounts.service"), exports);
20
+ __exportStar(require("./notifications.service"), exports);
20
21
  __exportStar(require("./storage.service"), exports);
21
22
  __exportStar(require("./mailer.service"), exports);
@@ -0,0 +1,7 @@
1
+ import { NotificationsApiClient } from "@maioradv/notifications-lib";
2
+ import { ConfigService } from "@nestjs/config";
3
+ export declare class NotificationsService {
4
+ private readonly configService;
5
+ client: NotificationsApiClient;
6
+ constructor(configService: ConfigService);
7
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.NotificationsService = void 0;
13
+ const notifications_lib_1 = require("@maioradv/notifications-lib");
14
+ const common_1 = require("@nestjs/common");
15
+ const config_1 = require("@nestjs/config");
16
+ let NotificationsService = class NotificationsService {
17
+ constructor(configService) {
18
+ this.configService = configService;
19
+ this.client = (0, notifications_lib_1.notificationsApiClient)(this.configService.get('maior.notifications') ?? { host: notifications_lib_1.ApiHost.main });
20
+ }
21
+ };
22
+ exports.NotificationsService = NotificationsService;
23
+ exports.NotificationsService = NotificationsService = __decorate([
24
+ (0, common_1.Injectable)(),
25
+ __metadata("design:paramtypes", [config_1.ConfigService])
26
+ ], NotificationsService);
@@ -6,3 +6,4 @@ export * from './time.helper';
6
6
  export * from './image.helper';
7
7
  export * from './email.helper';
8
8
  export * from './price.helper';
9
+ export * from './whatsapp.helper';
@@ -22,3 +22,4 @@ __exportStar(require("./time.helper"), exports);
22
22
  __exportStar(require("./image.helper"), exports);
23
23
  __exportStar(require("./email.helper"), exports);
24
24
  __exportStar(require("./price.helper"), exports);
25
+ __exportStar(require("./whatsapp.helper"), exports);
@@ -0,0 +1,19 @@
1
+ export declare class WhatsappBuilder {
2
+ basePath: string;
3
+ pattern: string;
4
+ group: string;
5
+ locale: string;
6
+ setGroup(group: string): this;
7
+ setPattern(pattern: string): this;
8
+ setLocale(locale: string): this;
9
+ private paths;
10
+ safeCheck(): Promise<void>;
11
+ private getNestedValue;
12
+ init(): Promise<{
13
+ template: HandlebarsTemplateDelegate<any>;
14
+ }>;
15
+ build(args?: Record<string, any>): Promise<{
16
+ message: string;
17
+ }>;
18
+ replace(ctx: string, replace?: Record<string, any>): string;
19
+ }
@@ -0,0 +1,88 @@
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.WhatsappBuilder = void 0;
7
+ const path_1 = require("path");
8
+ const handlebars_1 = __importDefault(require("handlebars"));
9
+ const promises_1 = require("fs/promises");
10
+ class WhatsappBuilder {
11
+ constructor() {
12
+ this.basePath = (0, path_1.join)(process.cwd(), 'templates', 'whatsapp');
13
+ }
14
+ setGroup(group) {
15
+ this.group = group;
16
+ return this;
17
+ }
18
+ setPattern(pattern) {
19
+ this.pattern = pattern;
20
+ return this;
21
+ }
22
+ setLocale(locale) {
23
+ this.locale = locale;
24
+ return this;
25
+ }
26
+ paths() {
27
+ const mainPath = (0, path_1.join)(this.basePath, this.group);
28
+ return [
29
+ (0, path_1.join)(mainPath, 'doc.hbs'),
30
+ (0, path_1.join)(mainPath, 'partials', `${this.pattern}.hbs`),
31
+ (0, path_1.join)(mainPath, 'locales', `${this.locale}.json`),
32
+ ];
33
+ }
34
+ async safeCheck() {
35
+ const paths = [
36
+ (0, path_1.join)(this.basePath, this.group),
37
+ ...this.paths()
38
+ ];
39
+ const results = await Promise.all(paths.map(p => (0, promises_1.stat)(p).then(() => true).catch(() => false)));
40
+ const missing = paths.filter((_, i) => !results[i]);
41
+ if (missing.length > 0)
42
+ throw new Error(`Missing paths: ${missing.join()}`);
43
+ }
44
+ getNestedValue(obj, path) {
45
+ // @ts-ignore
46
+ return path.split('.').reduce((acc, key) => acc?.[key], obj) ?? path;
47
+ }
48
+ async init() {
49
+ const [Container, Body, Labels] = await Promise.all(this.paths().map(path => (0, promises_1.readFile)(path, 'utf-8')));
50
+ const labels = JSON.parse(Labels);
51
+ handlebars_1.default.registerHelper('fullDateTime', function (date, locale) {
52
+ return new Date(date).toLocaleString(locale, { dateStyle: 'full', timeStyle: 'short' });
53
+ });
54
+ handlebars_1.default.registerHelper('ternary', function (condition, valTrue, valFalse) {
55
+ return condition ? valTrue : valFalse;
56
+ });
57
+ handlebars_1.default.registerHelper('t', (key, options) => {
58
+ const template = this.getNestedValue(labels, key);
59
+ if (options.hash && Object.keys(options.hash).length > 0) {
60
+ return template.replace(/\{\{(\w+)\}\}/g, (_, k) => options.hash[k] ?? `{{${k}}}`);
61
+ }
62
+ return template;
63
+ });
64
+ handlebars_1.default.registerPartial('body', Body);
65
+ const template = handlebars_1.default.compile(Container);
66
+ return {
67
+ template
68
+ };
69
+ }
70
+ async build(args) {
71
+ const { template } = await this.init();
72
+ return {
73
+ message: template({
74
+ ...args,
75
+ locale: this.locale
76
+ })
77
+ };
78
+ }
79
+ replace(ctx, replace) {
80
+ if (!replace)
81
+ return ctx;
82
+ var re = new RegExp(Object.keys(replace).join("|"), "gi");
83
+ return ctx.replace(re, function (matched) {
84
+ return replace[matched];
85
+ });
86
+ }
87
+ }
88
+ exports.WhatsappBuilder = WhatsappBuilder;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maioradv/nestjs-core",
3
- "version": "2.2.2",
3
+ "version": "2.2.4",
4
4
  "description": "NestJS helpers by MaiorADV",
5
5
  "repository": "https://github.com/maioradv/nestjs-core.git",
6
6
  "author": "Maior ADV Srl",
@@ -19,6 +19,7 @@
19
19
  "dependencies": {
20
20
  "@aws-sdk/client-s3": "^3",
21
21
  "@maioradv/accounts-lib": "^1.5",
22
+ "@maioradv/notifications-lib": "^1.0",
22
23
  "@nestjs/cache-manager": "^3",
23
24
  "@nestjs/common": "^11",
24
25
  "@nestjs/config": "^4",
@@ -52,6 +53,7 @@
52
53
  "peerDependencies": {
53
54
  "@aws-sdk/client-s3": "^3",
54
55
  "@maioradv/accounts-lib": "^1.5",
56
+ "@maioradv/notifications-lib": "^1",
55
57
  "@nestjs/cache-manager": "^3",
56
58
  "@nestjs/common": "^11",
57
59
  "@nestjs/config": "^4",