File: /var/www/api.javaapp.co.uk_old/src/services/auth/auth.services.js
import NotificationListDB from "../../models/notification_list.model.js";
import ResetPasswordDB from "../../models/resetpassword.model.js";
import UserDB from "../../models/users.model.js";
import { Op } from "sequelize";
import { createPresignedPost } from "@aws-sdk/s3-presigned-post";
import { S3Client } from "@aws-sdk/client-s3";
export async function findByEmail(userDetails) {
let userData = await UserDB.findOne({
attributes: [
"id",
"name",
"email",
"otp",
"profile_picture",
"address",
"notification_status",
"password",
"is_verified",
"is_active",
"is_deleted",
],
where: {
email: userDetails.email,
},
});
return userData;
}
export async function findById(userDetails) {
try {
let userData = await UserDB.findOne({
attributes: [
"id",
"name",
"email",
"otp",
"profile_picture",
"address",
"notification_status",
"password",
"is_verified",
"is_active",
"is_deleted",
"user_latitude",
"user_longitude",
],
where: {
id: userDetails.user_id,
},
});
return userData;
} catch (error) {
console.log(error);
throw new Error(error);
}
}
export async function userList() {
try {
return await UserDB.findAll();
} catch (error) {
throw new Error(error);
}
}
export async function updateUser(userDetails, id) {
try {
return await UserDB.update(userDetails, {
where: {
id: id,
},
});
} catch (error) {
throw new Error(error);
}
}
export async function updateUserByEmail(userDetails, email) {
try {
return await UserDB.update(userDetails, {
where: {
email: email,
},
});
} catch (error) {
throw new Error(error);
}
}
export async function createUser(userDetails) {
try {
await UserDB.create(userDetails);
return true;
} catch (error) {
throw new Error(error);
}
}
export async function getUserEmail(data) {
try {
var result = await UserDB.findOne({
where: {
[Op.and]: [
{
email: data.email,
},
{
is_verified: 1,
},
],
},
});
return result;
} catch (error) {
throw new Error(error);
}
}
export async function ForgotPasswordByEmail(email) {
let userData = await ResetPasswordDB.findOne({
where: {
email: email,
},
});
return userData;
}
export async function updateForgotPasswordByEmail(userDetails, email) {
try {
return await ResetPasswordDB.update(userDetails, {
where: {
email: email,
},
});
} catch (error) {
throw new Error(error);
}
}
export async function createForgotPassword(userDetails) {
try {
await ResetPasswordDB.create(userDetails);
return true;
} catch (error) {
throw new Error(error);
}
}
export async function getNotificationList(userDetails, query) {
try {
let getlist = await NotificationListDB.paginate({
page: query.page,
paginate: 10,
order: [["id", "ASC"]],
include: [
{
model: UserDB,
attributes: ["id", "name", "profile_picture"],
where: { is_deleted: 0, is_active: 1, is_verified: 1 },
},
],
where: {
receiver_id: userDetails.user_id,
notification_type: { [Op.ne]: 1 },
},
});
return getlist;
} catch (error) {
throw new Error(error);
}
}
export async function removeNotification(id, data) {
try {
await NotificationListDB.destroy({
where: {
receiver_id: id.user_id,
id: parseInt(data.id),
},
});
return true;
} catch (error) {
throw new Error(error);
}
}
export async function removeAllNotification(user_id) {
try {
await NotificationListDB.destroy({
where: {
receiver_id: user_id,
},
});
return true;
} catch (error) {
throw new Error(error);
}
}
export async function s3BucketDetails(file_type) {
try {
let clientParams = {
region: process.env.AWS_SES_REGION,
credentials: {
accessKeyId: process.env.AWS_SES_ACCESS_KEYID,
secretAccessKey: process.env.AWS_SES_SECRET_ACCESS_KEY
}
}
const prePath = 'signed-url-demo/';
const Key = `${prePath}${(Math.random() + 1).toString(36).substring(2) + file_type }`
let client = new S3Client(clientParams);
let res = createPresignedPost(client, {
Bucket: process.env.AWS_BUCKET_NAME,
Key,
Expires: 3600, //Seconds before the presigned post expires. 3600 by default.
});
return res;
} catch (error) {
throw new Error(error);
}
}