@maioradv/nestjs-core 2.3.8 → 2.3.9

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.
@@ -13,7 +13,7 @@ export declare class EmailBuilder {
13
13
  private paths;
14
14
  safeCheck(): Promise<void>;
15
15
  private getNestedValue;
16
- initEmail(): Promise<{
16
+ init(): Promise<{
17
17
  subject: string;
18
18
  template: HandlebarsTemplateDelegate<any>;
19
19
  }>;
@@ -57,7 +57,7 @@ class EmailBuilder {
57
57
  // @ts-ignore
58
58
  return path.split('.').reduce((acc, key) => acc?.[key], obj) ?? path;
59
59
  }
60
- async initEmail() {
60
+ async init() {
61
61
  const [Container, Header, Footer, Body, Labels] = await Promise.all(this.paths().map(path => (0, promises_1.readFile)(path, 'utf-8')));
62
62
  const labels = JSON.parse(Labels);
63
63
  const subject = this.getNestedValue(labels, `${this.pattern}.subject`);
@@ -84,7 +84,7 @@ class EmailBuilder {
84
84
  };
85
85
  }
86
86
  async build(args) {
87
- const { subject, template } = await this.initEmail();
87
+ const { subject, template } = await this.init();
88
88
  return {
89
89
  subject,
90
90
  html: template({
@@ -7,4 +7,5 @@ export * from './image.helper';
7
7
  export * from './email.helper';
8
8
  export * from './price.helper';
9
9
  export * from './whatsapp.helper';
10
+ export * from './push-notification.helper';
10
11
  export * from './where.helper';
@@ -23,4 +23,5 @@ __exportStar(require("./image.helper"), exports);
23
23
  __exportStar(require("./email.helper"), exports);
24
24
  __exportStar(require("./price.helper"), exports);
25
25
  __exportStar(require("./whatsapp.helper"), exports);
26
+ __exportStar(require("./push-notification.helper"), exports);
26
27
  __exportStar(require("./where.helper"), exports);
@@ -0,0 +1,21 @@
1
+ export declare class PushNotificationBuilder {
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
+ title: string;
14
+ template: HandlebarsTemplateDelegate<any>;
15
+ }>;
16
+ build(args?: Record<string, any>): Promise<{
17
+ title: string;
18
+ body: string;
19
+ }>;
20
+ replace(ctx: string, replace?: Record<string, any>): string;
21
+ }
@@ -0,0 +1,91 @@
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.PushNotificationBuilder = void 0;
7
+ const path_1 = require("path");
8
+ const handlebars_1 = __importDefault(require("handlebars"));
9
+ const promises_1 = require("fs/promises");
10
+ class PushNotificationBuilder {
11
+ constructor() {
12
+ this.basePath = (0, path_1.join)(process.cwd(), 'templates', 'push');
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
+ const title = this.getNestedValue(labels, `${this.pattern}.title`);
52
+ handlebars_1.default.registerHelper('fullDateTime', function (date, locale) {
53
+ return new Date(date).toLocaleString(locale, { dateStyle: 'full', timeStyle: 'short' });
54
+ });
55
+ handlebars_1.default.registerHelper('ternary', function (condition, valTrue, valFalse) {
56
+ return condition ? valTrue : valFalse;
57
+ });
58
+ handlebars_1.default.registerHelper('t', (key, options) => {
59
+ const template = this.getNestedValue(labels, key);
60
+ if (options.hash && Object.keys(options.hash).length > 0) {
61
+ return template.replace(/\{\{(\w+)\}\}/g, (_, k) => options.hash[k] ?? `{{${k}}}`);
62
+ }
63
+ return template;
64
+ });
65
+ handlebars_1.default.registerPartial('body', Body);
66
+ const template = handlebars_1.default.compile(Container);
67
+ return {
68
+ title,
69
+ template
70
+ };
71
+ }
72
+ async build(args) {
73
+ const { title, template } = await this.init();
74
+ return {
75
+ title,
76
+ body: template({
77
+ ...args,
78
+ locale: this.locale
79
+ })
80
+ };
81
+ }
82
+ replace(ctx, replace) {
83
+ if (!replace)
84
+ return ctx;
85
+ var re = new RegExp(Object.keys(replace).join("|"), "gi");
86
+ return ctx.replace(re, function (matched) {
87
+ return replace[matched];
88
+ });
89
+ }
90
+ }
91
+ exports.PushNotificationBuilder = PushNotificationBuilder;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maioradv/nestjs-core",
3
- "version": "2.3.8",
3
+ "version": "2.3.9",
4
4
  "description": "NestJS helpers by MaiorADV",
5
5
  "repository": "https://github.com/maioradv/nestjs-core.git",
6
6
  "author": "Maior ADV Srl",
@@ -19,7 +19,7 @@
19
19
  "dependencies": {
20
20
  "@aws-sdk/client-s3": "^3",
21
21
  "@maioradv/accounts-lib": "^1.5.7",
22
- "@maioradv/notifications-lib": "^1.1.1",
22
+ "@maioradv/notifications-lib": "^1.1.2",
23
23
  "@maioradv/types": "^0.0.7",
24
24
  "@nestjs/cache-manager": "^3",
25
25
  "@nestjs/common": "^11",