refactor: the user setting and change to be cateogry based
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
import { Column, Entity, OneToMany } from "typeorm";
|
||||
|
||||
import { UserSetting } from "./user-setting.entity";
|
||||
import { BaseEntity } from "../../../common/entities/base.entity";
|
||||
import { FarsiNotifSettingsEnum, NotifSettingsEnum } from "../enums/notif-settings.enum";
|
||||
|
||||
@Entity()
|
||||
export class NotifSetting extends BaseEntity {
|
||||
@Column({ type: "enum", enum: NotifSettingsEnum, unique: true })
|
||||
title: string;
|
||||
|
||||
@Column({ type: "enum", enum: FarsiNotifSettingsEnum })
|
||||
title_fa: string;
|
||||
|
||||
@OneToMany(() => UserSetting, (userSettings) => userSettings.notifSetting)
|
||||
userSettings: UserSetting[];
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { Column, Entity, OneToMany } from "typeorm";
|
||||
|
||||
import { UserSetting } from "./user-setting.entity";
|
||||
import { BaseEntity } from "../../../common/entities/base.entity";
|
||||
import { NotifCategory, NotifType } from "../enums/notif-settings.enum";
|
||||
|
||||
@Entity()
|
||||
export class NotificationSetting extends BaseEntity {
|
||||
@Column({ type: "enum", enum: NotifType, unique: true })
|
||||
type: NotifType;
|
||||
|
||||
@Column({ type: "enum", enum: NotifCategory })
|
||||
category: NotifCategory;
|
||||
|
||||
@Column({ type: "text" })
|
||||
description: string;
|
||||
|
||||
@OneToMany(() => UserSetting, (userSetting) => userSetting.notificationSetting)
|
||||
userSettings: UserSetting[];
|
||||
|
||||
// constructor(type: NotifType) {
|
||||
// super();
|
||||
// this.type = type;
|
||||
// this.category = NotifDescriptions[type].category;
|
||||
// this.description = NotifDescriptions[type].fa;
|
||||
// }
|
||||
}
|
||||
@@ -1,19 +1,22 @@
|
||||
import { Column, Entity, ManyToOne } from "typeorm";
|
||||
|
||||
import { NotifSetting } from "./notif-setting.entity";
|
||||
import { NotificationSetting } from "./notification-setting.entity";
|
||||
import { BaseEntity } from "../../../common/entities/base.entity";
|
||||
import { User } from "../../users/entities/user.entity";
|
||||
|
||||
@Entity()
|
||||
export class UserSetting extends BaseEntity {
|
||||
// @Column({ type: "boolean", default: true })
|
||||
// sms: boolean;
|
||||
|
||||
// @Column({ type: "boolean", default: false })
|
||||
// email: boolean;
|
||||
|
||||
@Column({ type: "boolean", default: true })
|
||||
sms: boolean;
|
||||
isActive: boolean;
|
||||
|
||||
@Column({ type: "boolean", default: false })
|
||||
email: boolean;
|
||||
|
||||
@ManyToOne(() => NotifSetting, (notifSetting) => notifSetting.userSettings)
|
||||
notifSetting: NotifSetting;
|
||||
@ManyToOne(() => NotificationSetting, (notifSetting) => notifSetting.userSettings, { nullable: false })
|
||||
notificationSetting: NotificationSetting;
|
||||
|
||||
@ManyToOne(() => User, (user) => user.settings, { nullable: false })
|
||||
user: User;
|
||||
|
||||
@@ -1,23 +1,56 @@
|
||||
export enum NotifSettingsEnum {
|
||||
ANNOUNCE_NOTIF = "ANNOUNCE_NOTIF",
|
||||
BILL_PAYMENT = "BILL_PAYMENT",
|
||||
CREATE_SERVICE = "CREATE_SERVICE",
|
||||
UNBLOCKING_SERVICE = "UNBLOCKING_SERVICE",
|
||||
ANSWER_TICKET = "ANSWER_TICKET",
|
||||
export enum NotifCategory {
|
||||
SUPPORT = "SUPPORT", //TICKET
|
||||
SERVICE = "SERVICE",
|
||||
INVOICE = "INVOICE",
|
||||
ACCOUNT = "ACCOUNT",
|
||||
FINANCE = "FINANCE", //WALLET AND ...
|
||||
// ANNOUNCEMENT = "ANNOUNCEMENT",
|
||||
}
|
||||
|
||||
export enum NotifType {
|
||||
// Account category
|
||||
USER_LOGIN = "USER_LOGIN",
|
||||
ANNOUNCEMENT = "ANNOUNCEMENT",
|
||||
|
||||
// Finance category
|
||||
WALLET_CHARGE = "WALLET_CHARGE",
|
||||
WALLET_DEDUCTION = "WALLET_DEDUCTION",
|
||||
|
||||
//Invoice
|
||||
BILL_INVOICE_REMINDER = "BILL_INVOICE_REMINDER",
|
||||
BILL_INVOICE = "BILL_INVOICE",
|
||||
CREATE_INVOICE = "CREATE_INVOICE",
|
||||
BILL_PAYMENT_REMINDER = "BILL_PAYMENT_REMINDER",
|
||||
BLOCKING_SERVICE = "BLOCKING_SERVICE",
|
||||
|
||||
// Service category
|
||||
CREATE_SERVICE = "CREATE_SERVICE",
|
||||
UNBLOCK_SERVICE = "UNBLOCK_SERVICE",
|
||||
BLOCK_SERVICE = "BLOCK_SERVICE",
|
||||
|
||||
// Support category
|
||||
ANSWER_TICKET = "ANSWER_TICKET",
|
||||
CREATE_TICKET = "CREATE_TICKET",
|
||||
}
|
||||
|
||||
export enum FarsiNotifSettingsEnum {
|
||||
ANNOUNCE_NOTIF = "اعلانات و اطلاعیه ها",
|
||||
BILL_PAYMENT = "پرداخت صورت حساب",
|
||||
CREATE_SERVICE = "ایجاد سرویس",
|
||||
UNBLOCKING_SERVICE = "رفع انسداد سرویس",
|
||||
ANSWER_TICKET = "پاسخ تیکت",
|
||||
CREATE_INVOICE = "ایجاد صورت حساب",
|
||||
BILL_PAYMENT_REMINDER = "یادآوری پرداخت صورت حساب",
|
||||
BLOCKING_SERVICE = "انسداد سرویس",
|
||||
CREATE_TICKET = "ثبت تیکت جدبد",
|
||||
}
|
||||
export const NotifDescriptions: Record<NotifType, { fa: string; category: NotifCategory }> = {
|
||||
// Account category
|
||||
[NotifType.USER_LOGIN]: { fa: "ورود به ناحیه کاربری", category: NotifCategory.ACCOUNT },
|
||||
[NotifType.ANNOUNCEMENT]: { fa: "اعلانات و اطلاعیه ها", category: NotifCategory.ACCOUNT },
|
||||
|
||||
// Finance category
|
||||
[NotifType.WALLET_CHARGE]: { fa: "شارژ کیف پول", category: NotifCategory.FINANCE },
|
||||
[NotifType.WALLET_DEDUCTION]: { fa: "کسر از کیف پول", category: NotifCategory.FINANCE },
|
||||
|
||||
// Invoice category
|
||||
[NotifType.CREATE_INVOICE]: { fa: "ایجاد صورت حساب", category: NotifCategory.INVOICE },
|
||||
[NotifType.BILL_INVOICE_REMINDER]: { fa: "یادآوری پرداخت صورت حساب", category: NotifCategory.INVOICE },
|
||||
[NotifType.BILL_INVOICE]: { fa: "پرداخت صورت حساب", category: NotifCategory.INVOICE },
|
||||
|
||||
// Service category
|
||||
[NotifType.CREATE_SERVICE]: { fa: "ایجاد سرویس", category: NotifCategory.SERVICE },
|
||||
[NotifType.UNBLOCK_SERVICE]: { fa: "رفع انسداد سرویس", category: NotifCategory.SERVICE },
|
||||
[NotifType.BLOCK_SERVICE]: { fa: "انسداد سرویس", category: NotifCategory.SERVICE },
|
||||
|
||||
// Support category
|
||||
[NotifType.ANSWER_TICKET]: { fa: "پاسخ تیکت", category: NotifCategory.SUPPORT },
|
||||
[NotifType.CREATE_TICKET]: { fa: "ثبت تیکت جدید", category: NotifCategory.SUPPORT },
|
||||
};
|
||||
|
||||
@@ -1,67 +1 @@
|
||||
import { NotifSettingsEnum } from "./notif-settings.enum";
|
||||
|
||||
export const userSettings = [
|
||||
{
|
||||
title: NotifSettingsEnum.ANNOUNCE_NOTIF,
|
||||
fields: {
|
||||
sms: true,
|
||||
email: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
title: NotifSettingsEnum.ANSWER_TICKET,
|
||||
fields: {
|
||||
sms: true,
|
||||
email: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
title: NotifSettingsEnum.BILL_PAYMENT,
|
||||
fields: {
|
||||
sms: true,
|
||||
email: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
title: NotifSettingsEnum.BILL_PAYMENT_REMINDER,
|
||||
fields: {
|
||||
sms: true,
|
||||
email: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
title: NotifSettingsEnum.BLOCKING_SERVICE,
|
||||
fields: {
|
||||
sms: true,
|
||||
email: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
title: NotifSettingsEnum.CREATE_INVOICE,
|
||||
fields: {
|
||||
sms: true,
|
||||
email: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
title: NotifSettingsEnum.CREATE_SERVICE,
|
||||
fields: {
|
||||
sms: true,
|
||||
email: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
title: NotifSettingsEnum.CREATE_TICKET,
|
||||
fields: {
|
||||
sms: true,
|
||||
email: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
title: NotifSettingsEnum.UNBLOCKING_SERVICE,
|
||||
fields: {
|
||||
sms: true,
|
||||
email: false,
|
||||
},
|
||||
},
|
||||
];
|
||||
export const userSettings = [];
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { QueryRunner } from "typeorm";
|
||||
import { In, QueryRunner } from "typeorm";
|
||||
|
||||
import { CommonMessage, SettingMessageEnum, UserMessage } from "../../../common/enums/message.enum";
|
||||
import { User } from "../../users/entities/user.entity";
|
||||
import { UpdateUserSettingsDto } from "../DTO/update-settings.dto";
|
||||
import { NotifSetting } from "../entities/notif-setting.entity";
|
||||
import { NotificationSetting } from "../entities/notification-setting.entity";
|
||||
import { UserSetting } from "../entities/user-setting.entity";
|
||||
import { userSettings } from "../enums/user-settings";
|
||||
import { NotifType } from "../enums/notif-settings.enum";
|
||||
import { UserSettingsRepository } from "../repositories/user-settings.repository";
|
||||
|
||||
@Injectable()
|
||||
@@ -15,66 +14,61 @@ export class UserSettingsService {
|
||||
//*+*******************************************
|
||||
|
||||
async createUserSettings(userId: string, queryRunner: QueryRunner) {
|
||||
const user = await queryRunner.manager.findOneBy(User, {
|
||||
id: userId,
|
||||
});
|
||||
const user = await queryRunner.manager.findOneBy(User, { id: userId });
|
||||
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
|
||||
|
||||
for (const setting of userSettings) {
|
||||
const notifSetting = await queryRunner.manager.findOneBy(NotifSetting, {
|
||||
title: setting.title,
|
||||
});
|
||||
if (!notifSetting) throw new BadRequestException(SettingMessageEnum.NOTIF_NOT_FOUND);
|
||||
const userSetting = queryRunner.manager.create(UserSetting, {
|
||||
...setting,
|
||||
notifSetting,
|
||||
const notifSettings = await queryRunner.manager.find(NotificationSetting, {
|
||||
where: { type: In(Object.values(NotifType)) },
|
||||
});
|
||||
|
||||
if (notifSettings.length !== Object.values(NotifType).length) throw new BadRequestException(SettingMessageEnum.NOTIF_NOT_FOUND);
|
||||
|
||||
const userSettings = notifSettings.map((notifSetting) =>
|
||||
queryRunner.manager.create(UserSetting, {
|
||||
isActive: true,
|
||||
notificationSetting: notifSetting,
|
||||
user,
|
||||
});
|
||||
await queryRunner.manager.save(userSetting);
|
||||
}
|
||||
return {
|
||||
message: CommonMessage.CREATED,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
await queryRunner.manager.save(UserSetting, userSettings);
|
||||
|
||||
return { message: CommonMessage.CREATED };
|
||||
}
|
||||
//*+*******************************************
|
||||
async getSettings(userId: string) {
|
||||
const settings = await this.userSettingsRepository.find({
|
||||
where: {
|
||||
user: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
relations: ["notifSetting"],
|
||||
order: {
|
||||
createdAt: "ASC",
|
||||
},
|
||||
});
|
||||
return { settings };
|
||||
const groupedSettings = await this.userSettingsRepository
|
||||
.createQueryBuilder("userSetting")
|
||||
.innerJoin("userSetting.notificationSetting", "notif")
|
||||
.where("userSetting.userId = :userId", { userId })
|
||||
.select([
|
||||
"notif.category AS category",
|
||||
"jsonb_agg(jsonb_build_object('id', userSetting.id, 'type', notif.type, 'description', notif.description, 'isActive', userSetting.isActive)) AS settings",
|
||||
])
|
||||
.groupBy("notif.category")
|
||||
.getRawMany();
|
||||
|
||||
// const settings = groupedSettings.reduce(
|
||||
// (acc, row) => {
|
||||
// acc[row.category] = row.settings;
|
||||
// return acc;
|
||||
// },
|
||||
// {} as Record<NotifCategory, any[]>,
|
||||
// );
|
||||
|
||||
return { settings: groupedSettings };
|
||||
}
|
||||
//*+*******************************************
|
||||
|
||||
async updateUserSetting(settingId: string, updateDto: UpdateUserSettingsDto) {
|
||||
const setting = await this.userSettingsRepository.findOne({
|
||||
where: {
|
||||
id: settingId,
|
||||
},
|
||||
order: {
|
||||
createdAt: "ASC",
|
||||
},
|
||||
});
|
||||
async toggleUserNotificationSetting(settingId: string) {
|
||||
const setting = await this.userSettingsRepository.findOneBy({ id: settingId });
|
||||
|
||||
if (!setting) throw new BadRequestException(SettingMessageEnum.NOT_FOUND);
|
||||
|
||||
if (updateDto.sms !== undefined) {
|
||||
setting.sms = updateDto.sms;
|
||||
}
|
||||
|
||||
if (updateDto.email !== undefined) {
|
||||
setting.email = updateDto.email;
|
||||
}
|
||||
setting.isActive = !setting.isActive;
|
||||
|
||||
await this.userSettingsRepository.save(setting);
|
||||
|
||||
return setting;
|
||||
return { message: CommonMessage.UPDATE_SUCCESS, setting };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@ import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
|
||||
import { NotifSetting } from "../entities/notif-setting.entity";
|
||||
import { NotificationSetting } from "../entities/notification-setting.entity";
|
||||
|
||||
@Injectable()
|
||||
export class NotifSettingsRepository extends Repository<NotifSetting> {
|
||||
constructor(@InjectRepository(NotifSetting) notifSettingsRepository: Repository<NotifSetting>) {
|
||||
export class NotifSettingsRepository extends Repository<NotificationSetting> {
|
||||
constructor(@InjectRepository(NotificationSetting) notifSettingsRepository: Repository<NotificationSetting>) {
|
||||
super(notifSettingsRepository.target, notifSettingsRepository.manager, notifSettingsRepository.queryRunner);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import { Body, Controller, Get, HttpCode, HttpStatus, Param, Patch } from "@nestjs/common";
|
||||
import { Controller, Get, HttpCode, HttpStatus, Param, Patch } from "@nestjs/common";
|
||||
import { ApiOperation, ApiTags } from "@nestjs/swagger";
|
||||
|
||||
import { UpdateUserSettingsDto } from "./DTO/update-settings.dto";
|
||||
import { UserSettingsService } from "./providers/user-settings.service";
|
||||
import { AuthGuards } from "../../common/decorators/auth-guard.decorator";
|
||||
import { UserDec } from "../../common/decorators/user.decorator";
|
||||
import { ParamDto } from "../../common/DTO/param.dto";
|
||||
import { User } from "../users/entities/user.entity";
|
||||
|
||||
@Controller("settings")
|
||||
@ApiTags("Settings")
|
||||
@@ -16,15 +14,15 @@ export class SettingsController {
|
||||
@AuthGuards()
|
||||
@ApiOperation({ summary: "Get all settings" })
|
||||
@Get()
|
||||
async getSettings(@UserDec() user: User) {
|
||||
return await this.userSettingsService.getSettings(user.id);
|
||||
async getSettings(@UserDec("id") userId: string) {
|
||||
return await this.userSettingsService.getSettings(userId);
|
||||
}
|
||||
|
||||
@AuthGuards()
|
||||
@ApiOperation({ summary: "update status of setting" })
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@Patch(":id/update")
|
||||
updateStatus(@Param() paramDto: ParamDto, @Body() createDto: UpdateUserSettingsDto) {
|
||||
return this.userSettingsService.updateUserSetting(paramDto.id, createDto);
|
||||
@Patch(":id/toggle")
|
||||
updateStatus(@Param() paramDto: ParamDto) {
|
||||
return this.userSettingsService.toggleUserNotificationSetting(paramDto.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
import { Module, forwardRef } from "@nestjs/common";
|
||||
import { Module } from "@nestjs/common";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
|
||||
import { NotifSetting } from "./entities/notif-setting.entity";
|
||||
import { NotificationSetting } from "./entities/notification-setting.entity";
|
||||
import { UserSetting } from "./entities/user-setting.entity";
|
||||
import { NotifSettingsService } from "./providers/notif-settings.service";
|
||||
import { UserSettingsService } from "./providers/user-settings.service";
|
||||
import { NotifSettingsRepository } from "./repositories/notif-settings.repository";
|
||||
import { UserSettingsRepository } from "./repositories/user-settings.repository";
|
||||
import { SettingsController } from "./settings.controller";
|
||||
import { UsersModule } from "../users/users.module";
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([UserSetting, NotifSetting]), forwardRef(() => UsersModule)],
|
||||
imports: [TypeOrmModule.forFeature([UserSetting, NotificationSetting])],
|
||||
providers: [UserSettingsService, NotifSettingsService, UserSettingsRepository, NotifSettingsRepository],
|
||||
controllers: [SettingsController],
|
||||
exports: [UserSettingsService, UserSettingsRepository],
|
||||
|
||||
Reference in New Issue
Block a user