admin
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsMobilePhone, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
import { IsArray, IsMobilePhone, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||||
|
import { NotifEvent } from 'src/modules/notification/interfaces/notification.interface';
|
||||||
|
|
||||||
export class CreateAdminDto {
|
export class CreateAdminDto {
|
||||||
@ApiProperty({ description: 'Mobile phone number' })
|
@ApiProperty({ description: 'Mobile phone number' })
|
||||||
@@ -22,4 +23,8 @@ export class CreateAdminDto {
|
|||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@IsString()
|
@IsString()
|
||||||
roleId!: string;
|
roleId!: string;
|
||||||
|
|
||||||
|
@ApiProperty({})
|
||||||
|
@IsArray()
|
||||||
|
notificationPrefrences:NotifEvent[]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { Entity, ManyToOne, OneToOne, PrimaryKey, Property } from '@mikro-orm/core';
|
import { Cascade, Entity, ManyToOne, OneToOne, PrimaryKey, Property } from '@mikro-orm/core';
|
||||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||||
import { normalizePhone } from '../../util/phone.util';
|
import { normalizePhone } from '../../util/phone.util';
|
||||||
import { Role } from 'src/modules/roles/entities/role.entity';
|
import { Role } from 'src/modules/roles/entities/role.entity';
|
||||||
import { ulid } from 'ulid';
|
import { ulid } from 'ulid';
|
||||||
|
import { NotificationPreference } from 'src/modules/notification/entities/notification-preference.entity';
|
||||||
|
|
||||||
@Entity({ tableName: 'admins' })
|
@Entity({ tableName: 'admins' })
|
||||||
export class Admin extends BaseEntity {
|
export class Admin extends BaseEntity {
|
||||||
@@ -29,4 +30,9 @@ export class Admin extends BaseEntity {
|
|||||||
|
|
||||||
@ManyToOne(() => Role)
|
@ManyToOne(() => Role)
|
||||||
role: Role
|
role: Role
|
||||||
|
|
||||||
|
// @OneToOne(() => NotificationPreference, pref => pref.admin, {
|
||||||
|
|
||||||
|
// })
|
||||||
|
// notificationPreference!: NotificationPreference;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import { BadRequestException, ConflictException, Injectable, NotFoundException } from '@nestjs/common';
|
import { BadRequestException, ConflictException, Injectable, NotFoundException } from '@nestjs/common';
|
||||||
import { Admin } from '../entities/admin.entity';
|
import { Admin } from '../entities/admin.entity';
|
||||||
import { Role } from '../../roles/entities/role.entity';
|
import { Role } from '../../roles/entities/role.entity';
|
||||||
import { EntityManager, RequiredEntityData } from '@mikro-orm/postgresql';
|
import { EntityManager } from '@mikro-orm/postgresql';
|
||||||
import { UpdateAdminDto } from '../dto/update-admin.dto';
|
import { UpdateAdminDto } from '../dto/update-admin.dto';
|
||||||
import { normalizePhone } from '../../util/phone.util';
|
import { normalizePhone } from '../../util/phone.util';
|
||||||
import { CreateAdminDto } from '../dto/create-admin.dto';
|
import { CreateAdminDto } from '../dto/create-admin.dto';
|
||||||
import { AdminRepository } from '../repositories/admin.repository';
|
import { AdminRepository } from '../repositories/admin.repository';
|
||||||
import { RoleRepository } from 'src/modules/roles/respository/role.repository';
|
import { RoleRepository } from 'src/modules/roles/respository/role.repository';
|
||||||
|
import { NotificationPreference } from 'src/modules/notification/entities/notification-preference.entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AdminService {
|
export class AdminService {
|
||||||
@@ -17,7 +18,7 @@ export class AdminService {
|
|||||||
) { }
|
) { }
|
||||||
|
|
||||||
async create(dto: CreateAdminDto) {
|
async create(dto: CreateAdminDto) {
|
||||||
const { phone, firstName, lastName, roleId } = dto;
|
const { phone, firstName, lastName, roleId, notificationPrefrences } = dto;
|
||||||
|
|
||||||
const normalizedPhone = normalizePhone(phone);
|
const normalizedPhone = normalizePhone(phone);
|
||||||
|
|
||||||
@@ -25,25 +26,34 @@ export class AdminService {
|
|||||||
phone: normalizedPhone,
|
phone: normalizedPhone,
|
||||||
});
|
});
|
||||||
if (exist) {
|
if (exist) {
|
||||||
throw new NotFoundException('This phone number is already used');
|
throw new BadRequestException('This phone number is already used');
|
||||||
}
|
}
|
||||||
|
|
||||||
const role = await this.roleRepository.findOne({ id: roleId })
|
const role = await this.roleRepository.findOne({ id: roleId })
|
||||||
if (!role) {
|
if (!role) {
|
||||||
throw new NotFoundException('Role not found');
|
throw new BadRequestException('Role not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
const createData: RequiredEntityData<Admin> = {
|
|
||||||
|
return this.em.transactional(async em => {
|
||||||
|
|
||||||
|
const admin = em.create(Admin, {
|
||||||
phone: normalizedPhone,
|
phone: normalizedPhone,
|
||||||
firstName,
|
firstName,
|
||||||
lastName,
|
lastName,
|
||||||
role
|
role
|
||||||
}
|
})
|
||||||
const admin = this.adminRepository.create(createData)
|
|
||||||
|
|
||||||
await this.em.persistAndFlush(admin);
|
const notificationPreference = em.create(NotificationPreference, {
|
||||||
|
admin,
|
||||||
|
events: notificationPrefrences,
|
||||||
|
})
|
||||||
|
|
||||||
|
await this.em.persistAndFlush([admin, notificationPreference]);
|
||||||
|
|
||||||
|
return admin
|
||||||
|
})
|
||||||
|
|
||||||
return admin;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async findByPhone(phone: string): Promise<Admin | null> {
|
async findByPhone(phone: string): Promise<Admin | null> {
|
||||||
@@ -82,6 +92,18 @@ export class AdminService {
|
|||||||
if (rest.lastName !== undefined) {
|
if (rest.lastName !== undefined) {
|
||||||
admin.lastName = rest.lastName;
|
admin.lastName = rest.lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (rest.notificationPrefrences) {
|
||||||
|
//
|
||||||
|
const nf = await this.em.findOne(NotificationPreference, {
|
||||||
|
admin
|
||||||
|
})
|
||||||
|
if (!nf) {
|
||||||
|
throw new BadRequestException('Notification prefrences not found!')
|
||||||
|
}
|
||||||
|
nf.events = rest.notificationPrefrences
|
||||||
|
await this.em.flush()
|
||||||
|
}
|
||||||
if (rest.phone !== undefined && rest.phone !== admin.phone) {
|
if (rest.phone !== undefined && rest.phone !== admin.phone) {
|
||||||
const normalizedPhone = normalizePhone(rest.phone);
|
const normalizedPhone = normalizePhone(rest.phone);
|
||||||
const exists = await this.adminRepository.findOne({ phone: normalizedPhone });
|
const exists = await this.adminRepository.findOne({ phone: normalizedPhone });
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
import { IsNotEmpty, IsEnum, IsArray } from 'class-validator';
|
import { IsNotEmpty, IsEnum, IsArray } from 'class-validator';
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { NotifTitleEnum } from '../interfaces/notification.interface';
|
import { NotifEvent } from '../interfaces/notification.interface';
|
||||||
import { NotifChannelEnum } from '../interfaces/notification.interface';
|
import { NotifChannelEnum } from '../interfaces/notification.interface';
|
||||||
|
|
||||||
export class CreatePreferenceDto {
|
export class CreatePreferenceDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Notification title/type (e.g., order.created, review.created)',
|
description: 'Notification title/type (e.g., order.created, review.created)',
|
||||||
enum: NotifTitleEnum,
|
enum: NotifEvent,
|
||||||
example: NotifTitleEnum.ORDER_CREATED,
|
example: NotifEvent.NEW_ORDER,
|
||||||
})
|
})
|
||||||
@IsEnum(NotifTitleEnum)
|
@IsEnum(NotifEvent)
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
title!: NotifTitleEnum;
|
title!: NotifEvent;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Notification type (SMS, PUSH, BOTH, or NONE)',
|
description: 'Notification type (SMS, PUSH, BOTH, or NONE)',
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
import { Entity, Property, ManyToOne, Unique, PrimaryKey } from '@mikro-orm/core';
|
import { Entity, Property, ManyToOne, Unique, PrimaryKey, OneToOne, Cascade } from '@mikro-orm/core';
|
||||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||||
import { NotifTitleEnum } from '../interfaces/notification.interface';
|
import { NotifEvent } from '../interfaces/notification.interface';
|
||||||
import { NotifChannelEnum } from '../interfaces/notification.interface';
|
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
||||||
|
|
||||||
@Entity({ tableName: 'notification_preferences' })
|
@Entity({ tableName: 'notification_preferences' })
|
||||||
export class NotificationPreference extends BaseEntity {
|
export class NotificationPreference extends BaseEntity {
|
||||||
@PrimaryKey({ type: 'bigint', autoincrement: true })
|
@PrimaryKey({ type: 'int', autoincrement: true })
|
||||||
id: bigint
|
id: number
|
||||||
|
|
||||||
@Property()
|
@OneToOne(() => Admin, { owner: true, unique: true, cascade: [Cascade.PERSIST, Cascade.REMOVE] })
|
||||||
title!: NotifTitleEnum;
|
admin!: Admin;
|
||||||
|
|
||||||
@Property({ type: 'json' })
|
@Property({ type: 'json', nullable: true })
|
||||||
channels: NotifChannelEnum[] = [];
|
events: NotifEvent[] = [];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Entity, Property, ManyToOne, Enum, PrimaryKey } from '@mikro-orm/core';
|
import { Entity, Property, ManyToOne, Enum, PrimaryKey } from '@mikro-orm/core';
|
||||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||||
import { User } from '../../user/entities/user.entity';
|
import { User } from '../../user/entities/user.entity';
|
||||||
import { NotifTitleEnum } from '../interfaces/notification.interface';
|
import { NotifEvent } from '../interfaces/notification.interface';
|
||||||
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
||||||
|
|
||||||
@Entity({ tableName: 'notifications' })
|
@Entity({ tableName: 'notifications' })
|
||||||
@@ -15,8 +15,8 @@ export class Notification extends BaseEntity {
|
|||||||
@ManyToOne(() => Admin, { nullable: true })
|
@ManyToOne(() => Admin, { nullable: true })
|
||||||
admin?: Admin;
|
admin?: Admin;
|
||||||
|
|
||||||
@Enum(() => NotifTitleEnum)
|
@Enum(() => NotifEvent)
|
||||||
title!: NotifTitleEnum;
|
title!: NotifEvent;
|
||||||
|
|
||||||
@Property()
|
@Property()
|
||||||
content!: string;
|
content!: string;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { NotifTitleEnum, recipientType } from './notification.interface';
|
import type { NotifEvent, recipientType } from './notification.interface';
|
||||||
|
|
||||||
export interface SmsNotificationQueueJob {
|
export interface SmsNotificationQueueJob {
|
||||||
recipient: recipientType;
|
recipient: recipientType;
|
||||||
@@ -18,7 +18,7 @@ export interface PushNotificationQueueJob {
|
|||||||
}
|
}
|
||||||
export interface InAppNotificationQueueJob {
|
export interface InAppNotificationQueueJob {
|
||||||
recipient: { adminId: string; };
|
recipient: { adminId: string; };
|
||||||
subject: NotifTitleEnum;
|
subject: NotifEvent;
|
||||||
body: string;
|
body: string;
|
||||||
notificationId: string;
|
notificationId: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,17 +8,17 @@ export enum NotifTypeEnum {
|
|||||||
PROMOTIONAL = 'promotional',
|
PROMOTIONAL = 'promotional',
|
||||||
SYSTEM = 'system',
|
SYSTEM = 'system',
|
||||||
}
|
}
|
||||||
export enum NotifTitleEnum {
|
export enum NotifEvent {
|
||||||
PAGER_CREATED = 'pager.created',
|
NEW_REQUEST = 'newRequest',
|
||||||
ORDER_CREATED = 'order.created',
|
INVOICED = 'invoiced',
|
||||||
PAYMENT_SUCCESS = 'payment.success',
|
NEW_ORDER = 'newOrder', // for chapkhane sms bere
|
||||||
REVIEW_CREATED = 'review.created',
|
DESIGNER_ASSIGN = 'designerAssign',
|
||||||
ORDER_STATUS_CHANGED = 'order.status.changed',
|
PAYMENT_SUCCESS = 'paymentSuccess',
|
||||||
}
|
}
|
||||||
export type recipientType = { userId: string } | { adminId: string };
|
export type recipientType = { userId: string } | { adminId: string };
|
||||||
|
|
||||||
export interface NotifRequestMessage {
|
export interface NotifRequestMessage {
|
||||||
title: NotifTitleEnum;
|
title: NotifEvent;
|
||||||
content: string;
|
content: string;
|
||||||
sms: {
|
sms: {
|
||||||
templateId: string;
|
templateId: string;
|
||||||
@@ -53,11 +53,11 @@ interface INotifySmsPayload {
|
|||||||
}
|
}
|
||||||
interface INotifySms {
|
interface INotifySms {
|
||||||
phone: string;
|
phone: string;
|
||||||
subject: NotifTitleEnum;
|
subject: NotifEvent;
|
||||||
}
|
}
|
||||||
export interface IInAppNotificationPayload {
|
export interface IInAppNotificationPayload {
|
||||||
notificationId: string;
|
notificationId: string;
|
||||||
subject: NotifTitleEnum;
|
subject: NotifEvent;
|
||||||
body: string;
|
body: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ interface IPaymentSuccessSmsNotifyPayload extends INotifySmsPayload {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface IPaymentSuccessSmsNotify extends INotifySms {
|
interface IPaymentSuccessSmsNotify extends INotifySms {
|
||||||
subject: NotifTitleEnum.PAYMENT_SUCCESS;
|
subject: NotifEvent.PAYMENT_SUCCESS;
|
||||||
payload: IPaymentSuccessSmsNotifyPayload;
|
payload: IPaymentSuccessSmsNotifyPayload;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { EntityManager } from '@mikro-orm/postgresql';
|
|||||||
import { NotificationPreference } from '../entities/notification-preference.entity';
|
import { NotificationPreference } from '../entities/notification-preference.entity';
|
||||||
import { CreatePreferenceDto } from '../dto/create-preference.dto';
|
import { CreatePreferenceDto } from '../dto/create-preference.dto';
|
||||||
import { UpdatePreferenceDto } from '../dto/update-preference.dto';
|
import { UpdatePreferenceDto } from '../dto/update-preference.dto';
|
||||||
import { NotifTitleEnum } from '../interfaces/notification.interface';
|
import { NotifEvent } from '../interfaces/notification.interface';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class NotificationPreferenceService {
|
export class NotificationPreferenceService {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Notification } from '../entities/notification.entity';
|
|||||||
import { NotificationPreferenceService } from './notification-preference.service';
|
import { NotificationPreferenceService } from './notification-preference.service';
|
||||||
import { NotificationQueueService } from './notification-queue.service';
|
import { NotificationQueueService } from './notification-queue.service';
|
||||||
import { NotificationsGateway } from '../notifications.gateway';
|
import { NotificationsGateway } from '../notifications.gateway';
|
||||||
import { NotifChannelEnum, NotifRequest, NotifTitleEnum } from '../interfaces/notification.interface';
|
import { NotifChannelEnum, NotifRequest, NotifEvent } from '../interfaces/notification.interface';
|
||||||
import { SmsLog } from '../entities/smsLogs.entity';
|
import { SmsLog } from '../entities/smsLogs.entity';
|
||||||
import { SmsLogRepository } from '../repositories/sms-log.repository';
|
import { SmsLogRepository } from '../repositories/sms-log.repository';
|
||||||
import { PaginatedResult } from '../../../common/interfaces/pagination.interface';
|
import { PaginatedResult } from '../../../common/interfaces/pagination.interface';
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { OnEvent } from '@nestjs/event-emitter';
|
|||||||
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
|
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
|
||||||
import { OrderCreatedEvent, OrderStatusChangedEvent } from '../events/order.events';
|
import { OrderCreatedEvent, OrderStatusChangedEvent } from '../events/order.events';
|
||||||
import { Permission } from 'src/common/enums/permission.enum';
|
import { Permission } from 'src/common/enums/permission.enum';
|
||||||
import { NotifTitleEnum } from 'src/modules/notification/interfaces/notification.interface';
|
import { NotifEvent } from 'src/modules/notification/interfaces/notification.interface';
|
||||||
import { NotificationService } from 'src/modules/notification/services/notification.service';
|
import { NotificationService } from 'src/modules/notification/services/notification.service';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { OrderRepository } from '../repositories/order.repository';
|
import { OrderRepository } from '../repositories/order.repository';
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { OnEvent } from '@nestjs/event-emitter';
|
|||||||
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
|
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
|
||||||
import { onlinePaymentSucceedEvent, paymentSucceedEvent } from '../events/payment.events';
|
import { onlinePaymentSucceedEvent, paymentSucceedEvent } from '../events/payment.events';
|
||||||
import { Permission } from 'src/common/enums/permission.enum';
|
import { Permission } from 'src/common/enums/permission.enum';
|
||||||
import { NotifTitleEnum } from 'src/modules/notification/interfaces/notification.interface';
|
import { NotifEvent } from 'src/modules/notification/interfaces/notification.interface';
|
||||||
import { NotificationService } from 'src/modules/notification/services/notification.service';
|
import { NotificationService } from 'src/modules/notification/services/notification.service';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { OrderService } from 'src/modules/order/providers/order.service';
|
import { OrderService } from 'src/modules/order/providers/order.service';
|
||||||
|
|||||||
@@ -1,29 +1,29 @@
|
|||||||
import { NotifChannelEnum, NotifTitleEnum } from '../../modules/notification/interfaces/notification.interface';
|
import { NotifChannelEnum, NotifEvent } from '../../modules/notification/interfaces/notification.interface';
|
||||||
|
|
||||||
export interface NotificationPreferenceData {
|
export interface NotificationPreferenceData {
|
||||||
title: NotifTitleEnum;
|
title: NotifEvent;
|
||||||
channels: NotifChannelEnum[];
|
channels: NotifChannelEnum[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export const notificationPreferencesData: NotificationPreferenceData[] = [
|
export const notificationPreferencesData: NotificationPreferenceData[] = [
|
||||||
{
|
// {
|
||||||
title: NotifTitleEnum.PAGER_CREATED,
|
// title: NotifEvent.PAGER_CREATED,
|
||||||
channels: [NotifChannelEnum.SMS, NotifChannelEnum.PUSH, NotifChannelEnum.IN_APP],
|
// channels: [NotifChannelEnum.SMS, NotifChannelEnum.PUSH, NotifChannelEnum.IN_APP],
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
title: NotifTitleEnum.ORDER_CREATED,
|
// title: NotifEvent.ORDER_CREATED,
|
||||||
channels: [NotifChannelEnum.SMS, NotifChannelEnum.PUSH, NotifChannelEnum.IN_APP],
|
// channels: [NotifChannelEnum.SMS, NotifChannelEnum.PUSH, NotifChannelEnum.IN_APP],
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
title: NotifTitleEnum.PAYMENT_SUCCESS,
|
// title: NotifEvent.PAYMENT_SUCCESS,
|
||||||
channels: [NotifChannelEnum.SMS, NotifChannelEnum.PUSH, NotifChannelEnum.IN_APP],
|
// channels: [NotifChannelEnum.SMS, NotifChannelEnum.PUSH, NotifChannelEnum.IN_APP],
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
title: NotifTitleEnum.REVIEW_CREATED,
|
// title: NotifEvent.REVIEW_CREATED,
|
||||||
channels: [NotifChannelEnum.SMS, NotifChannelEnum.PUSH, NotifChannelEnum.IN_APP],
|
// channels: [NotifChannelEnum.SMS, NotifChannelEnum.PUSH, NotifChannelEnum.IN_APP],
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
title: NotifTitleEnum.ORDER_STATUS_CHANGED,
|
// title: NotifEvent.ORDER_STATUS_CHANGED,
|
||||||
channels: [NotifChannelEnum.SMS, NotifChannelEnum.PUSH, NotifChannelEnum.IN_APP],
|
// channels: [NotifChannelEnum.SMS, NotifChannelEnum.PUSH, NotifChannelEnum.IN_APP],
|
||||||
},
|
// },
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user