-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
100 lines (81 loc) · 3.36 KB
/
helper.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const mailer = require('nodemailer')
const jwt = require('jsonwebtoken')
const Str = require('@supercharge/strings')
require('dotenv').config()
const pwStrength = /^(?=.*[A-Za-z])(?=.*\d)[\S]{6,}$/
const fs = require('fs');
const path = require('path')
const logPath = "./.logs/"
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
module.exports = {
testPasswordStrength: function (password) {
return pwStrength.test(password)
},
createJWT: function (id, email, username, handle) {
return jwt.sign({ id, email, username, handle }, process.env.JWT_SECRET, {
expiresIn: '1y'
})
},
generateRandomString: function () {
return Str.random(90)
},
saveLog: function (actionToLog, handle) {
const today = new Date();
const yyyy = today.getFullYear();
let mm = today.getMonth() + 1;
let dd = today.getDate();
if (dd < 10) dd = '0' + dd;
if (mm < 10) mm = '0' + mm;
const formattedToday = yyyy + '.' + mm + '.' + dd;
let formattedTime = new Date();
formattedTime = formattedTime.getHours() + ":" + formattedTime.getMinutes() + ":" + formattedTime.getSeconds();
console.log(formattedToday + " - " + formattedTime + " | " + handle + " | " + actionToLog);
fs.appendFile(logPath + formattedToday + ".nbl", "\n"+(formattedToday + " - " + formattedTime + " | " + handle + " | " + actionToLog), function (err) {
if (err) throw err;
});
},
async getTimeStamp() {
const now = new Date();
const utcOffset = now.getTimezoneOffset() * 1000; // convert offset to milliseconds
const localTime = new Date(now.getTime() - utcOffset + (3600 * 1000)); // adjust for UTC+1
return localTime
},
async sendNotification (notificationType, senderID, recieverID, postID = undefined){
console.log(postID)
already_exists = await prisma.notifications.findFirst({
where: {
Type: notificationType?.type,
SenderID: senderID,
RecieverID: recieverID,
PostID: postID
}
})
if(senderID == recieverID && notificationType?.type != 'comment' || already_exists) return
const now = new Date();
const utcOffset = now.getTimezoneOffset() * 1000; // convert offset to milliseconds
const localTime = new Date(now.getTime() - utcOffset + (3600 * 1000)); // adjust for UTC+1
send = await prisma.notifications.create({
data: {
Type: notificationType?.type,
SenderID: senderID,
RecieverID: recieverID,
PostID: postID,
DateCreated: localTime
}
})
},
notifications: Object.freeze({ follower: {type: 'follower', message: ' is now following you.'}, mention: {type: 'mention', message: ' has mentioned you in their post.'}, like: {type: 'like', message: ' has liked your post.'}, comment: {type: 'comment', message: ' has commented your post.'}}),
resSend (res, data, status, errors) {
data = data ?? {}
status = status?.toString() ?? this.resStatuses.ok
errors = errors ?? []
if (!Array.isArray(errors)) errors = [errors]
const rspJson = {}
rspJson.status = status
rspJson.errors = errors
rspJson.data = data
res.send(JSON.stringify(rspJson))
},
resStatuses: Object.freeze({ ok: 'OK', error: 'Error' })
}