@skalfa/skalfa-api-core 1.0.28 → 1.0.30
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/dist/auth/create-user-mail-token.js +2 -2
- package/dist/auth/create-user-mail-token.js.map +1 -1
- package/dist/auth/verify-user-mail-token.js +1 -5
- package/dist/auth/verify-user-mail-token.js.map +1 -1
- package/dist/mail/mail.d.ts +1 -1
- package/dist/mail/mail.js +21 -4
- package/dist/mail/mail.js.map +1 -1
- package/package.json +1 -1
- package/src/auth/create-user-mail-token.ts +3 -3
- package/src/auth/verify-user-mail-token.ts +1 -6
- package/src/mail/mail.ts +25 -4
|
@@ -4,13 +4,13 @@ export async function createUserMailToken(userId) {
|
|
|
4
4
|
const token = Math.floor(100000 + Math.random() * 900000).toString();
|
|
5
5
|
const hash = crypto.createHash('sha256').update(token).digest('hex');
|
|
6
6
|
const trx = await db.transaction();
|
|
7
|
+
await trx.table('user_mail_tokens').where('user_id', userId).whereNull("used_at").delete();
|
|
7
8
|
await trx.table('user_mail_tokens').insert({
|
|
8
9
|
user_id: userId,
|
|
9
10
|
token: hash,
|
|
10
11
|
created_at: new Date(),
|
|
11
12
|
});
|
|
12
|
-
const record = await trx.table('user_mail_tokens').orderBy('id', 'desc').first();
|
|
13
|
-
await trx.commit();
|
|
13
|
+
const record = await trx.table('user_mail_tokens').where('user_id', userId).orderBy('id', 'desc').first();
|
|
14
14
|
return {
|
|
15
15
|
token: token,
|
|
16
16
|
tokenId: record.id
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-user-mail-token.js","sourceRoot":"","sources":["../../src/auth/create-user-mail-token.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAA;AAC3B,OAAO,EAAE,EAAE,EAAE,MAAM,oBAAoB,CAAA;AAEvC,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,MAAc;IACtD,MAAM,KAAK,GAAG,IAAI,CAAC,KA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GAAG,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC;QACzC,OAAO,EAAO,MAAM;QACpB,KAAK,EAAS,IAAI;QAClB,UAAU,EAAI,IAAI,IAAI,EAAE;KACzB,CAAC,CAAA;IAEF,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC,KAAK,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,KAAK,EAAE,CAAA;IAEzG,OAAO;QACL,KAAK,EAAM,KAAK;QAChB,OAAO,EAAI,MAAM,CAAC,EAAE;KACrB,CAAA;AACH,CAAC"}
|
|
@@ -2,11 +2,7 @@ import crypto from 'crypto';
|
|
|
2
2
|
import { db } from '@skalfa/skalfa-orm';
|
|
3
3
|
export async function verifyUserMailToken(userId, token) {
|
|
4
4
|
const hashedToken = crypto.createHash("sha256").update(token).digest("hex");
|
|
5
|
-
const record = await db.table("user_mail_tokens")
|
|
6
|
-
.where("user_id", userId)
|
|
7
|
-
.whereNull("used_at")
|
|
8
|
-
.orderBy("id", "desc")
|
|
9
|
-
.first();
|
|
5
|
+
const record = await db.table("user_mail_tokens").where("user_id", userId).whereNull("used_at").orderBy("id", "desc").first();
|
|
10
6
|
if (!record)
|
|
11
7
|
return false;
|
|
12
8
|
if (record.token !== hashedToken)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verify-user-mail-token.js","sourceRoot":"","sources":["../../src/auth/verify-user-mail-token.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAA;AAC3B,OAAO,EAAE,EAAE,EAAE,MAAM,oBAAoB,CAAA;AAEvC,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,MAAc,EAAE,KAAa;IACrE,MAAM,WAAW,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAE5E,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,KAAK,CAAC,kBAAkB,CAAC
|
|
1
|
+
{"version":3,"file":"verify-user-mail-token.js","sourceRoot":"","sources":["../../src/auth/verify-user-mail-token.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAA;AAC3B,OAAO,EAAE,EAAE,EAAE,MAAM,oBAAoB,CAAA;AAEvC,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,MAAc,EAAE,KAAa;IACrE,MAAM,WAAW,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAE5E,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC,KAAK,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,KAAK,EAAE,CAAC;IAC9H,IAAI,CAAC,MAAM;QAAE,OAAO,KAAK,CAAA;IAEzB,IAAI,MAAM,CAAC,KAAK,KAAK,WAAW;QAAE,OAAO,KAAK,CAAC;IAE/C,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IAC9C,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC;IACvB,MAAM,WAAW,GAAG,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,SAAS,CAAC,OAAO,EAAE,CAAC,GAAG,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC;IAExE,IAAI,WAAW,GAAG,EAAE;QAAE,OAAO,KAAK,CAAC;IAEnC,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/dist/mail/mail.d.ts
CHANGED
|
@@ -18,4 +18,4 @@ export declare function sendMail(options: {
|
|
|
18
18
|
path: string;
|
|
19
19
|
}[];
|
|
20
20
|
}): Promise<any>;
|
|
21
|
-
export declare function renderMailTemplate(template: string, options: Record<string, string
|
|
21
|
+
export declare function renderMailTemplate(template: string, options: Record<string, string>, customDir?: string): string;
|
package/dist/mail/mail.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { readFileSync } from "fs";
|
|
1
|
+
import { readFileSync, existsSync } from "fs";
|
|
2
2
|
import { join } from "path";
|
|
3
3
|
import nodemailer from "nodemailer";
|
|
4
4
|
import { logger } from "@utils";
|
|
@@ -29,15 +29,32 @@ export async function sendMail(options) {
|
|
|
29
29
|
// =====================================>
|
|
30
30
|
// ## Mail: Render mail template
|
|
31
31
|
// =====================================>
|
|
32
|
-
export function renderMailTemplate(template, options) {
|
|
33
|
-
const
|
|
32
|
+
export function renderMailTemplate(template, options, customDir) {
|
|
33
|
+
const appTemplateDir = join(process.cwd(), "app", "outputs", "mails", "templates");
|
|
34
|
+
const coreTemplateDir = join(import.meta.dir, "./../outputs/mails/templates");
|
|
35
|
+
let templateDir = customDir;
|
|
36
|
+
if (!templateDir) {
|
|
37
|
+
if (existsSync(join(appTemplateDir, `${template}.mail.stub`))) {
|
|
38
|
+
templateDir = appTemplateDir;
|
|
39
|
+
}
|
|
40
|
+
else if (existsSync(join(coreTemplateDir, `${template}.mail.stub`))) {
|
|
41
|
+
templateDir = coreTemplateDir;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
templateDir = appTemplateDir;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
34
47
|
const contentPath = join(templateDir, `${template}.mail.stub`);
|
|
48
|
+
if (!existsSync(contentPath)) {
|
|
49
|
+
throw new Error(`Mail template '${template}.mail.stub' not found at: ${contentPath}`);
|
|
50
|
+
}
|
|
35
51
|
let content = readFileSync(contentPath, "utf-8");
|
|
36
52
|
for (const [key, value] of Object.entries(options)) {
|
|
37
53
|
const regex = new RegExp(`{{\\s*${key}\\s*}}`, "g");
|
|
38
54
|
content = content.replace(regex, value);
|
|
39
55
|
}
|
|
40
|
-
|
|
56
|
+
const layoutPath = join(templateDir, "layout.mail.stub");
|
|
57
|
+
let layout = existsSync(layoutPath) ? readFileSync(layoutPath, "utf-8") : "{{content}}";
|
|
41
58
|
const globalVars = {
|
|
42
59
|
...options,
|
|
43
60
|
date: "20-10-2025",
|
package/dist/mail/mail.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mail.js","sourceRoot":"","sources":["../../src/mail/mail.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"mail.js","sourceRoot":"","sources":["../../src/mail/mail.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,UAA+B,MAAM,YAAY,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAiBhC,yCAAyC;AACzC,qBAAqB;AACrB,yCAAyC;AACzC,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,OAM9B;IACC,MAAM,WAAW,GAAG,UAAU,CAAC,eAAe,CAAC;QAC7C,IAAI,EAAO,OAAO,CAAC,GAAG,CAAC,SAAS;QAChC,IAAI,EAAO,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC;QACxC,MAAM,EAAK,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,GAAG;QAChD,IAAI,EAAO;YACT,IAAI,EAAK,OAAO,CAAC,GAAG,CAAC,aAAa;YAClC,IAAI,EAAK,OAAO,CAAC,GAAG,CAAC,aAAa;SACnC;KACF,CAAC,CAAC;IAEH,MAAM,IAAI,GAAG,CAAC,MAAM,WAAW,CAAC,QAAQ,CAAC;QACvC,IAAI,EAAY,GAAG,OAAO,CAAC,GAAG,CAAC,cA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/package.json
CHANGED
|
@@ -7,15 +7,15 @@ export async function createUserMailToken(userId: number) {
|
|
|
7
7
|
const hash = crypto.createHash('sha256').update(token).digest('hex')
|
|
8
8
|
const trx = await db.transaction()
|
|
9
9
|
|
|
10
|
+
await trx.table('user_mail_tokens').where('user_id', userId).whereNull("used_at").delete()
|
|
11
|
+
|
|
10
12
|
await trx.table('user_mail_tokens').insert({
|
|
11
13
|
user_id : userId,
|
|
12
14
|
token : hash,
|
|
13
15
|
created_at : new Date(),
|
|
14
16
|
})
|
|
15
17
|
|
|
16
|
-
const record = await trx.table('user_mail_tokens').orderBy('id', 'desc').first()
|
|
17
|
-
|
|
18
|
-
await trx.commit()
|
|
18
|
+
const record = await trx.table('user_mail_tokens').where('user_id', userId).orderBy('id', 'desc').first()
|
|
19
19
|
|
|
20
20
|
return {
|
|
21
21
|
token : token,
|
|
@@ -4,12 +4,7 @@ import { db } from '@skalfa/skalfa-orm'
|
|
|
4
4
|
export async function verifyUserMailToken(userId: number, token: string) {
|
|
5
5
|
const hashedToken = crypto.createHash("sha256").update(token).digest("hex");
|
|
6
6
|
|
|
7
|
-
const record = await db.table("user_mail_tokens")
|
|
8
|
-
.where("user_id", userId)
|
|
9
|
-
.whereNull("used_at")
|
|
10
|
-
.orderBy("id", "desc")
|
|
11
|
-
.first();
|
|
12
|
-
|
|
7
|
+
const record = await db.table("user_mail_tokens").where("user_id", userId).whereNull("used_at").orderBy("id", "desc").first();
|
|
13
8
|
if (!record) return false
|
|
14
9
|
|
|
15
10
|
if (record.token !== hashedToken) return false;
|
package/src/mail/mail.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { readFileSync } from "fs";
|
|
1
|
+
import { readFileSync, existsSync } from "fs";
|
|
2
2
|
import { join } from "path";
|
|
3
3
|
import nodemailer, { SentMessageInfo } from "nodemailer";
|
|
4
4
|
import { logger } from "@utils";
|
|
@@ -56,10 +56,30 @@ export async function sendMail(options: {
|
|
|
56
56
|
// =====================================>
|
|
57
57
|
// ## Mail: Render mail template
|
|
58
58
|
// =====================================>
|
|
59
|
-
export function renderMailTemplate(
|
|
60
|
-
|
|
59
|
+
export function renderMailTemplate(
|
|
60
|
+
template: string,
|
|
61
|
+
options: Record<string, string>,
|
|
62
|
+
customDir?: string
|
|
63
|
+
) {
|
|
64
|
+
const appTemplateDir = join(process.cwd(), "app", "outputs", "mails", "templates");
|
|
65
|
+
const coreTemplateDir = join(import.meta.dir, "./../outputs/mails/templates");
|
|
66
|
+
|
|
67
|
+
let templateDir = customDir;
|
|
68
|
+
if (!templateDir) {
|
|
69
|
+
if (existsSync(join(appTemplateDir, `${template}.mail.stub`))) {
|
|
70
|
+
templateDir = appTemplateDir;
|
|
71
|
+
} else if (existsSync(join(coreTemplateDir, `${template}.mail.stub`))) {
|
|
72
|
+
templateDir = coreTemplateDir;
|
|
73
|
+
} else {
|
|
74
|
+
templateDir = appTemplateDir;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
61
77
|
|
|
62
78
|
const contentPath = join(templateDir, `${template}.mail.stub`);
|
|
79
|
+
if (!existsSync(contentPath)) {
|
|
80
|
+
throw new Error(`Mail template '${template}.mail.stub' not found at: ${contentPath}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
63
83
|
let content = readFileSync(contentPath, "utf-8");
|
|
64
84
|
|
|
65
85
|
for (const [key, value] of Object.entries(options)) {
|
|
@@ -67,7 +87,8 @@ export function renderMailTemplate(template: string, options: Record<string, str
|
|
|
67
87
|
content = content.replace(regex, value);
|
|
68
88
|
}
|
|
69
89
|
|
|
70
|
-
|
|
90
|
+
const layoutPath = join(templateDir, "layout.mail.stub");
|
|
91
|
+
let layout = existsSync(layoutPath) ? readFileSync(layoutPath, "utf-8") : "{{content}}";
|
|
71
92
|
|
|
72
93
|
const globalVars = {
|
|
73
94
|
...options,
|