File size: 2,137 Bytes
4dbde5b
 
 
 
 
 
0159174
 
 
 
4dbde5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b38d67a
4dbde5b
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import crypto from "crypto";

export const AUTH_COOKIE_NAME = "sales_system_session";

const SEVEN_DAYS_IN_MS = 7 * 24 * 60 * 60 * 1000;

export const createSessionToken = () => ({
   token: crypto.randomBytes(32).toString("hex"),
   expiresAt: new Date(Date.now() + SEVEN_DAYS_IN_MS),
});

export const getAuthCookieOptions = () => ({
   httpOnly: true,
   sameSite: "none",
   secure: process.env.NODE_ENV === "production",
   maxAge: SEVEN_DAYS_IN_MS,
   path: "/",
});

export const getClearCookieOptions = () => ({
   httpOnly: true,
   sameSite: "none",
   secure: process.env.NODE_ENV === "production",
   path: "/",
});

export const setAuthCookie = (res, token) => {
   res.cookie(AUTH_COOKIE_NAME, token, getAuthCookieOptions());
};

export const clearAuthCookie = (res) => {
   res.clearCookie(AUTH_COOKIE_NAME, getClearCookieOptions());
};

export const parseCookies = (cookieHeader = "") =>
   cookieHeader.split(";").reduce((cookies, rawCookie) => {
      const [name, ...valueParts] = rawCookie.trim().split("=");

      if (!name) {
         return cookies;
      }

      cookies[name] = decodeURIComponent(valueParts.join("="));
      return cookies;
   }, {});

export const toSlug = (value = "") =>
   value
      .toString()
      .trim()
      .toLowerCase()
      .replace(/[^a-z0-9]+/g, "-")
      .replace(/^-+|-+$/g, "")
      .replace(/-{2,}/g, "-");

export const buildTenantCredentialsWhatsApp = ({

   ownerName,

   username,

   temporaryPassword,

   loginUrl,

}) => {
   const message = [
      `مرحباً ${ownerName || "صاحب النشاط"}`,
      `تم إنشاء حساب نشاطك التجاري على منصة دفترى.`,
      `اسم المستخدم: ${username}`,
      `كلمة المرور المؤقتة: ${temporaryPassword}`,
      `رابط الدخول: ${loginUrl}`,
      "يرجى تسجيل الدخول وتغيير كلمة المرور بعد أول استخدام.",
   ].join("\n");

   return {
      message,
      whatsappLink: `https://wa.me/?text=${encodeURIComponent(message)}`,
   };
};