ztechno_core 0.0.96 → 0.0.98

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.
@@ -4,6 +4,7 @@ import {
4
4
  MailOptionsText,
5
5
  MailResponse,
6
6
  MailServiceOptions,
7
+ ZMailBlacklist,
7
8
  ZMailSendOptTemplate,
8
9
  } from './typings';
9
10
  import { ZMailBlacklistOrm } from './orm/mail_blacklist_orm';
@@ -11,12 +12,13 @@ export declare class ZMailService {
11
12
  private opt;
12
13
  protected get sql(): import('./sql_service').ZSQLService;
13
14
  protected blacklistOrm: ZMailBlacklistOrm;
14
- private blacklist;
15
+ protected blacklist: ZMailBlacklist[];
15
16
  /**
16
17
  * Creates a new ZMailService instance
17
18
  * @param opt - Configuration options for the mail service including authentication, sender email, and SQL service
18
19
  */
19
20
  constructor(opt: MailServiceOptions);
21
+ updateBlacklist(opt: Pick<ZMailBlacklist, 'email' | 'is_blacklisted' | 'hash'>): Promise<void>;
20
22
  /**
21
23
  * Refreshes the email blacklist from the database
22
24
  * @returns Promise that resolves when the blacklist has been refreshed
@@ -27,6 +27,16 @@ class ZMailService {
27
27
  ? this.opt.dirTemplate
28
28
  : path_1.default.join(process.cwd(), this.opt.dirTemplate || '');
29
29
  }
30
+ async updateBlacklist(opt) {
31
+ const newItem = await this.blacklistOrm.upsert(opt);
32
+ const found = this.blacklist.find((item) => item.email === opt.email);
33
+ if (found) {
34
+ found.is_blacklisted = opt.is_blacklisted;
35
+ return;
36
+ }
37
+ this.blacklist.push(newItem);
38
+ return;
39
+ }
30
40
  /**
31
41
  * Refreshes the email blacklist from the database
32
42
  * @returns Promise that resolves when the blacklist has been refreshed
@@ -114,7 +124,7 @@ class ZMailService {
114
124
  }
115
125
  if (opts.inject !== undefined) {
116
126
  const key = opts.html !== undefined ? 'html' : 'body';
117
- const baseInject = { email: mailOpts.recipient, hashToUnsubscribe };
127
+ const baseInject = Object.assign({ email: mailOpts.recipient, hashToUnsubscribe }, this.opt.baseInject ?? {});
118
128
  opts[key] = await this.inject(opts[key], Object.assign(baseInject, opts.inject));
119
129
  }
120
130
  return await this.send(opts);
@@ -17,5 +17,6 @@ export declare class ZMailBlacklistOrm extends ZOrm {
17
17
  },
18
18
  ): Promise<ZMailBlacklist[]>;
19
19
  update({ email, is_blacklisted }: Pick<ZMailBlacklist, 'email' | 'is_blacklisted'>): Promise<boolean>;
20
+ upsert(item: Omit<ZMailBlacklist, 'updated_at' | 'created_at'>): Promise<ZMailBlacklist>;
20
21
  createTable(): Promise<void>;
21
22
  }
@@ -51,6 +51,17 @@ class ZMailBlacklistOrm extends orm_1.ZOrm {
51
51
  );
52
52
  return res.affectedRows !== 0;
53
53
  }
54
+ async upsert(item) {
55
+ await this.sqlService.query(
56
+ `
57
+ INSERT INTO \`${this.alias}\` (email, hash, is_blacklisted, updated_at, created_at)
58
+ VALUES (:email, :hash, :is_blacklisted, NOW(), NOW())
59
+ ON DUPLICATE KEY UPDATE hash=:hash, is_blacklisted=:is_blacklisted, updated_at=NOW()
60
+ `,
61
+ item,
62
+ );
63
+ return item;
64
+ }
54
65
  async createTable() {
55
66
  await this.sqlService.query(/*SQL*/ `
56
67
  CREATE TABLE \`${this.alias}\` (
@@ -39,6 +39,12 @@ export type MailServiceOptions = {
39
39
  sqlService: ZSQLService;
40
40
  hashSalt?: string;
41
41
  dirTemplate?: string;
42
+ baseInject?: {
43
+ logoSrc?: string;
44
+ baseUrl?: string;
45
+ title: string;
46
+ content: string;
47
+ };
42
48
  };
43
49
  export type MailOptionsBase = {
44
50
  recipient: string;
@@ -81,7 +87,7 @@ export type ZMailSendOptAll = MailOptionsBase & {
81
87
  export type ZMailBlacklist = {
82
88
  email: string;
83
89
  hash?: string;
84
- is_blacklisted: 0 | 1;
90
+ is_blacklisted: 0 | 1 | boolean;
85
91
  updated_at: string;
86
92
  created_at: string;
87
93
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ztechno_core",
3
- "version": "0.0.96",
3
+ "version": "0.0.98",
4
4
  "description": "Core files for ztechno framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",