Untitled
unknown
javascript
2 years ago
6.0 kB
13
Indexable
import { LIMIT, ROLE } from "../common/constant";
import Pagination from "../interfaces/pagination.interface";
import {
ICreateProposal,
IUpdateProposal,
} from "../interfaces/proposal/proposal.interface";
import { UserAuth } from "../interfaces/user/user-auth.interface";
import PostModel from "../models/post.model";
import UserModel from "../models/user.model";
import ProposalModel from "../models/proposal.model";
import NotificationModel from "../models/notification.model";
import { pagination } from "./base.service";
import paymentHistoryService from "./paymentHistory.service";
import { TRANSACTION_TYPE } from "../interfaces/transaction/transaction.enum";
import { PROPOSAL_STATE } from "../interfaces/proposal/proposal.enum";
import { Op } from "sequelize";
import { ICreateNotification } from "../interfaces/notification/notification.interface";
import { NOTIFICATION_ACTION } from "../interfaces/notification/notification.enum";
class NotificationService {
async getAll(filter: Pagination, user?: UserAuth) {
const { page = 1, limit = LIMIT } = filter;
const getNotification = NotificationModel.findAll({
limit: limit,
offset: page * limit - limit,
where: {
[Op.and]: [{ userIdTo: user?.id }, { isDelete: false }],
},
});
const countNotification = NotificationModel.count({});
const [notification, count] = await Promise.all([
getNotification,
countNotification,
]);
return {
totalPage: pagination(count, limit),
currentPage: page,
data: notification,
};
}
async getById(pid: string, user?: UserAuth) {
const notification = await NotificationModel.findOne({
where: {
[Op.and]: [{ _id: pid }, { isDelete: false }],
},
});
if (!notification) {
throw new Error("Thông báo không tồn tại!");
}
return {
data: notification,
};
}
async createNotification(data: ICreateNotification) {
try {
const receiver = await UserModel.findOne({
where: {
_id: data.userIdTo,
},
});
let sender;
if (data.userIdFrom) {
sender = await UserModel.findOne({
where: {
_id: data.userIdFrom,
},
});
}
if (!receiver) {
throw new Error("Người dùng không tồn tại");
}
let text = "";
switch (data.action) {
case NOTIFICATION_ACTION.APPLY_PROPOSAL:
text =
(((sender?.get("firstName") as string) +
sender?.get("lastName")) as string) +
" đã " +
NOTIFICATION_ACTION.APPLY_PROPOSAL +
" cho bạn";
break;
case NOTIFICATION_ACTION.ACCEPT_PROPOSAL:
text =
(((sender?.get("firstName") as string) +
sender?.get("lastName")) as string) +
" đã " +
NOTIFICATION_ACTION.ACCEPT_PROPOSAL +
" của bạn";
break;
case NOTIFICATION_ACTION.HIRE:
text =
(((sender?.get("firstName") as string) +
sender?.get("lastName")) as string) +
" đã gửi lời đề nghị" +
NOTIFICATION_ACTION.HIRE +
" bạn";
break;
case NOTIFICATION_ACTION.CREATE_CONTRACT_REQUEST:
text =
(((sender?.get("firstName") as string) +
sender?.get("lastName")) as string) +
" đã " +
NOTIFICATION_ACTION.CREATE_CONTRACT_REQUEST +
" với bạn";
break;
case NOTIFICATION_ACTION.APPROVE_CONTRACT:
text =
(((sender?.get("firstName") as string) +
sender?.get("lastName")) as string) +
" đã " +
NOTIFICATION_ACTION.APPROVE_CONTRACT +
" với bạn";
break;
case NOTIFICATION_ACTION.DONE_TASK:
text =
(((sender?.get("firstName") as string) +
sender?.get("lastName")) as string) +
" đã " +
NOTIFICATION_ACTION.DONE_TASK;
break;
case NOTIFICATION_ACTION.CANCEL_CONTRACT:
text =
(((sender?.get("firstName") as string) +
sender?.get("lastName")) as string) +
" đã " +
NOTIFICATION_ACTION.CANCEL_CONTRACT +
" với bạn";
break;
case NOTIFICATION_ACTION.RATING:
text =
(((sender?.get("firstName") as string) +
sender?.get("lastName")) as string) +
" đã " +
NOTIFICATION_ACTION.RATING +
" cho bạn";
break;
case NOTIFICATION_ACTION.PUBLIC_PROFILE:
text = "Bạn đã " + NOTIFICATION_ACTION.PUBLIC_PROFILE + " của bạn";
break;
case NOTIFICATION_ACTION.ADD_MONEY:
text =
"Bạn vừa được " +
NOTIFICATION_ACTION.ADD_MONEY +
(data.money && Math.abs(data.money)) +
" xu";
break;
case NOTIFICATION_ACTION.DEDUCTED:
text =
"Bạn đã " +
NOTIFICATION_ACTION.PUBLIC_PROFILE +
(data.money && Math.abs(data.money)) +
" xu";
break;
default:
text = "Chào mừng!!";
break;
}
const { money, ...newData } = data;
newData.content = text;
const notification = await NotificationModel.create({
...newData,
});
return notification;
} catch (error) {
throw error;
}
}
async updateById(
data: IUpdateProposal,
id?: string,
fileName?: string,
user?: UserAuth
) {
// return {
// data: proposal,
// };
}
async deleteById(id: string) {
await NotificationModel.update(
{
isDelete: true,
},
{
where: {
_id: id,
},
}
);
return "Xóa thông báo thành công";
}
}
export default new NotificationService();Editor is loading...