mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2026-03-10 18:41:46 +00:00
- Added KycNotificationListener to handle notifications for KYC approval and rejection events. - Introduced CardNotificationListener to manage notifications for card creation and blocking events. - Enhanced CardService to emit events for card creation and blocking, integrating with the new notification system. - Updated notification constants and interfaces to include new KYC and card-related events. - Improved notification message formatting and added localization support for new events.
99 lines
3.4 KiB
TypeScript
99 lines
3.4 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { Roles } from '~/auth/enums/roles.enum';
|
|
import { CreateMoneyRequestDto, MoneyRequestsFiltersRequestDto } from '../dtos/request';
|
|
import { MoneyRequest } from '../entities/money-request.entity';
|
|
import { MoneyRequestStatus } from '../enums';
|
|
const FIRST_PAGE = 1;
|
|
@Injectable()
|
|
export class MoneyRequestsRepository {
|
|
constructor(@InjectRepository(MoneyRequest) private readonly moneyRequestRepository: Repository<MoneyRequest>) {}
|
|
|
|
findAll(userId: string, role: Roles, filters: MoneyRequestsFiltersRequestDto): Promise<[MoneyRequest[], number]> {
|
|
const queryBuilder = this.moneyRequestRepository.createQueryBuilder('moneyRequest');
|
|
|
|
if (role === Roles.JUNIOR) {
|
|
queryBuilder.where('moneyRequest.juniorId = :userId', { userId });
|
|
} else if (role === Roles.GUARDIAN) {
|
|
queryBuilder.where('moneyRequest.guardianId = :userId', { userId });
|
|
}
|
|
|
|
queryBuilder.leftJoinAndSelect('moneyRequest.junior', 'junior');
|
|
queryBuilder.leftJoinAndSelect('junior.customer', 'customer');
|
|
queryBuilder.leftJoinAndSelect('customer.user', 'user');
|
|
queryBuilder.leftJoinAndSelect('user.profilePicture', 'profilePicture');
|
|
|
|
if (filters.status) {
|
|
queryBuilder.andWhere('moneyRequest.status = :status', { status: filters.status });
|
|
}
|
|
|
|
queryBuilder.skip((filters.page - FIRST_PAGE) * filters.size);
|
|
queryBuilder.take(filters.size);
|
|
|
|
queryBuilder.orderBy('moneyRequest.createdAt', 'DESC');
|
|
return queryBuilder.getManyAndCount();
|
|
}
|
|
|
|
createMoneyRequest(juniorId: string, guardianId: string, body: CreateMoneyRequestDto): Promise<MoneyRequest> {
|
|
return this.moneyRequestRepository.save(
|
|
this.moneyRequestRepository.create({
|
|
amount: body.amount,
|
|
reason: body.reason,
|
|
status: MoneyRequestStatus.PENDING,
|
|
juniorId,
|
|
guardianId,
|
|
}),
|
|
);
|
|
}
|
|
|
|
findById(id: string, userId?: string, role?: Roles): Promise<MoneyRequest | null> {
|
|
const whereCondition: any = { id };
|
|
if (role === Roles.JUNIOR) {
|
|
whereCondition.juniorId = userId;
|
|
} else {
|
|
whereCondition.guardianId = userId;
|
|
}
|
|
return this.moneyRequestRepository.findOne({
|
|
where: whereCondition,
|
|
relations: [
|
|
'junior',
|
|
'junior.customer',
|
|
'junior.customer.user',
|
|
'junior.customer.user.profilePicture',
|
|
],
|
|
});
|
|
}
|
|
|
|
findByIdWithAllRelations(id: string, userId?: string, role?: Roles): Promise<MoneyRequest | null> {
|
|
const whereCondition: any = { id };
|
|
if (role === Roles.JUNIOR) {
|
|
whereCondition.juniorId = userId;
|
|
} else {
|
|
whereCondition.guardianId = userId;
|
|
}
|
|
return this.moneyRequestRepository.findOne({
|
|
where: whereCondition,
|
|
relations: [
|
|
'junior',
|
|
'junior.customer',
|
|
'junior.customer.user',
|
|
'junior.customer.user.profilePicture',
|
|
'junior.customer.cards',
|
|
'junior.customer.cards.account',
|
|
'guardian',
|
|
'guardian.customer',
|
|
'guardian.customer.user',
|
|
],
|
|
});
|
|
}
|
|
|
|
approveMoneyRequest(id: string) {
|
|
return this.moneyRequestRepository.update({ id }, { status: MoneyRequestStatus.APPROVED });
|
|
}
|
|
|
|
rejectMoneyRequest(id: string, rejectionReason?: string) {
|
|
return this.moneyRequestRepository.update({ id }, { status: MoneyRequestStatus.REJECTED, rejectionReason });
|
|
}
|
|
}
|