File: //lib/node_modules/java-go/src/services/auth/auth.services.js
import NotificationListDB from "../../models/notification_list.model.js"
import GroupCoffeeRunDB from "../../models/group_coffee_run.model.js"
import ResetPasswordDB from "../../models/resetpassword.model.js"
import { createPresignedPost } from "@aws-sdk/s3-presigned-post"
import appVersionDB from "../../models/app_versions.model.js"
import UserDB from "../../models/users.model.js"
import { S3Client } from "@aws-sdk/client-s3"
import { Op } from "sequelize"
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"
],
where: {
id: userDetails.user_id,
},
});
return userData;
} catch (error) {
console.log(error);
throw new Error(error);
}
}
export async function appVersionDetails(os_type) {
try {
let userData = await appVersionDB.findOne({
attributes: ['id', 'force_update_status', 'os_type', 'version', 'normal_update_message', 'force_update_message'],
where: {
os_type: os_type,
},
});
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,
},
],
},
});
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", "DESC"]],
include: [
{
model: UserDB,
attributes: ["id", "name", "profile_picture"],
where: { is_deleted: 0, is_active: 1, is_verified: 1 },
},
{
model: GroupCoffeeRunDB,
as: 'group_coffee_run_data',
attributes: ["cafe_id", "group_id", "request_endtime"],
required: false
}
],
where: {
receiver_id: userDetails.user_id,
notification_type: { [Op.ne]: 1 },
},
});
for (let i = 0; i < getList.docs.length; i++) {
await NotificationListDB.update(
{ is_read: 1 },
{
where: {
id: getList.docs[i].id
},
}
);
}
return getList;
} catch (error) {
throw new Error(error);
}
}
export async function getNotificationCount(user_id) {
try {
let count = await NotificationListDB.count({
where: {
receiver_id: user_id,
notification_type: { [Op.ne]: 1 },
is_read: 0
},
});
return count;
} 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);
}
}