mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2026-03-10 18:41:46 +00:00
Compare commits
29 Commits
652359b1bf
...
feature/al
| Author | SHA1 | Date | |
|---|---|---|---|
| f7f22de65c | |||
| 6e11812925 | |||
| 4d7549d02e | |||
| 799b9b883d | |||
| 1a0bd0bf91 | |||
| 52fb5f3984 | |||
| db946b9531 | |||
| d34508dca9 | |||
| 25ede3c9e7 | |||
| 47b825c4b2 | |||
| f5c3b03264 | |||
| 6a250efd5e | |||
| a09b84e475 | |||
| 604cb7ce25 | |||
| 4305c4b75f | |||
| ef5572440c | |||
| 64623c7cea | |||
| 4ca8123a67 | |||
| 7c9e0f0b51 | |||
| e734060c52 | |||
| 1086166e04 | |||
| 5e6c8d96de | |||
| 6d6dc1471f | |||
| 0f56381703 | |||
| 1b0d6cb284 | |||
| 887bd20217 | |||
| c963b57904 | |||
| 2e21acac7f | |||
| 145e6c62b8 |
291
ALLOWANCE_SCHEDULING_SYSTEM.md
Normal file
291
ALLOWANCE_SCHEDULING_SYSTEM.md
Normal file
@ -0,0 +1,291 @@
|
||||
# Allowance Scheduling System (Backend)
|
||||
|
||||
This document captures the complete allowance scheduling feature as implemented in the backend.
|
||||
It is intended as a long-term reference for how the system works, why it was built this way,
|
||||
and how to operate/extend it safely.
|
||||
|
||||
## Goals and Scope
|
||||
|
||||
- Allow parents/guardians to create a recurring allowance schedule for a child.
|
||||
- Credit the allowance automatically based on the schedule.
|
||||
- Scale safely with multiple workers and avoid duplicate credits.
|
||||
- Keep cron lightweight and offload work to RabbitMQ workers.
|
||||
|
||||
Non-goals in the current implementation:
|
||||
- UI changes
|
||||
- Analytics/reporting views
|
||||
- Advanced scheduling (e.g., custom weekdays)
|
||||
|
||||
## High-Level Flow
|
||||
|
||||
1. Parent creates a schedule via API.
|
||||
2. Cron runs every 5 minutes and enqueues due schedules into RabbitMQ.
|
||||
3. Workers consume queue messages, credit the allowance, and update the next run.
|
||||
4. Idempotency is enforced in the database to prevent duplicate credits.
|
||||
|
||||
## Data Model
|
||||
|
||||
### Table: `allowance_schedules`
|
||||
|
||||
Purpose: Stores the schedule definition (amount, frequency, status, next run).
|
||||
|
||||
Key fields:
|
||||
- `guardian_id`, `junior_id`: who funds and who receives.
|
||||
- `amount`: the allowance amount.
|
||||
- `frequency`: DAILY / WEEKLY / MONTHLY.
|
||||
- `status`: ON / OFF.
|
||||
- `next_run_at`, `last_run_at`: scheduling metadata.
|
||||
|
||||
Constraints:
|
||||
- Unique `(guardian_id, junior_id)` ensures one schedule per child.
|
||||
|
||||
Entity: `src/allowance/entities/allowance-schedule.entity.ts`
|
||||
|
||||
### Table: `allowance_credits`
|
||||
|
||||
Purpose: Audit log and idempotency guard for each credit run.
|
||||
|
||||
Key fields:
|
||||
- `schedule_id`: which schedule executed.
|
||||
- `transaction_id`: the resulting transaction (nullable).
|
||||
- `amount`, `run_at`, `credited_at`.
|
||||
|
||||
Idempotency:
|
||||
- Unique `(schedule_id, run_at)` prevents duplicates even with multiple workers.
|
||||
|
||||
Entity: `src/allowance/entities/allowance-credit.entity.ts`
|
||||
|
||||
### Table: `cron_runs`
|
||||
|
||||
Purpose: shared audit table for all cron jobs (not just allowance).
|
||||
|
||||
Key fields:
|
||||
- `job_name`: unique identifier for the cron.
|
||||
- `status`: SUCCESS / FAILED.
|
||||
- `processed_count`: number of schedules processed.
|
||||
- `error_message`: failure reason.
|
||||
- `started_at`, `finished_at`.
|
||||
|
||||
Entity: `src/cron/entities/cron-run.entity.ts`
|
||||
|
||||
## API (Schedule Creation)
|
||||
|
||||
Endpoint:
|
||||
- `POST /guardians/me/allowances/:juniorId`
|
||||
|
||||
Input DTO:
|
||||
- `amount` (required, numeric, positive)
|
||||
- `frequency` (required, enum)
|
||||
- `status` (required, enum)
|
||||
|
||||
DTO: `src/allowance/dtos/request/create-allowance-schedule.request.dto.ts`
|
||||
|
||||
Response DTO:
|
||||
- `AllowanceScheduleResponseDto` with schedule fields.
|
||||
|
||||
DTO: `src/allowance/dtos/response/allowance-schedule.response.dto.ts`
|
||||
|
||||
Business validation:
|
||||
- Child must belong to guardian.
|
||||
- No duplicate schedule for the same guardian+child.
|
||||
|
||||
Service: `src/allowance/services/allowance.service.ts`
|
||||
|
||||
## Cron Producer (Queue Enqueue)
|
||||
|
||||
Cron job:
|
||||
- Runs every 5 minutes.
|
||||
- Batches schedules (cursor-based) to avoid large load.
|
||||
- Enqueues each schedule to RabbitMQ.
|
||||
|
||||
Cron: `src/cron/tasks/allowance-schedule.cron.ts`
|
||||
|
||||
Batch behavior:
|
||||
- Batch size = 100
|
||||
- Uses a cursor (`nextRunAt`, `id`) for stable pagination.
|
||||
- Prevents re-reading the same rows.
|
||||
|
||||
Locking:
|
||||
- `BaseCronService` uses cache lock to ensure only one instance runs.
|
||||
- Each cron run is logged to `cron_runs` with status and processed count.
|
||||
|
||||
Lock: `src/cron/services/base-cron.service.ts`
|
||||
|
||||
## Queue Publisher
|
||||
|
||||
Queue publisher:
|
||||
- Asserts queue + retry/DLQ exchanges.
|
||||
- Enqueues jobs with message id (for traceability).
|
||||
|
||||
Service: `src/allowance/services/allowance-queue.service.ts`
|
||||
|
||||
Queue names:
|
||||
- `allowance.schedule` (main)
|
||||
- `allowance.schedule.retry` (retry with TTL)
|
||||
- `allowance.schedule.dlq` (dead-letter queue)
|
||||
|
||||
Constants: `src/allowance/constants/allowance-queue.constants.ts`
|
||||
|
||||
## Worker Consumer (Transfers)
|
||||
|
||||
Worker:
|
||||
- Consumes `allowance.schedule` queue.
|
||||
- Validates schedule is due and active.
|
||||
- Creates `allowance_credits` record for idempotency.
|
||||
- Transfers money via `cardService.transferToChild`.
|
||||
- Updates `last_run_at` and `next_run_at`.
|
||||
|
||||
Worker: `src/allowance/services/allowance-worker.service.ts`
|
||||
|
||||
Transfer:
|
||||
- Uses existing logic in `card.service.ts` for balance updates and transaction creation.
|
||||
|
||||
Service: `src/card/services/card.service.ts`
|
||||
|
||||
### Idempotency details
|
||||
|
||||
1. Worker inserts `allowance_credits` row first.
|
||||
2. Unique constraint blocks duplicates.
|
||||
3. If transfer fails, the credit row is removed so the job can retry.
|
||||
|
||||
This makes multiple workers safe.
|
||||
|
||||
## Retry + DLQ Strategy
|
||||
|
||||
Retry delay:
|
||||
- Failed jobs are dead-lettered to retry queue.
|
||||
- Retry queue has `messageTtl = 10 minutes`.
|
||||
- After TTL, job is routed back to main queue.
|
||||
|
||||
DLQ:
|
||||
- If a job fails `ALLOWANCE_MAX_RETRIES` times (default 5), it is routed to the DLQ.
|
||||
- This prevents endless loops and allows manual inspection.
|
||||
|
||||
Config:
|
||||
- `ALLOWANCE_MAX_RETRIES` (default 5)
|
||||
|
||||
## Redis Usage
|
||||
|
||||
Redis is used by `BaseCronService` to enforce a **distributed lock** for the cron job.
|
||||
This prevents multiple backend instances from enqueuing the same schedules at the same time.
|
||||
|
||||
Lock behavior:
|
||||
- If the lock key exists, cron exits early.
|
||||
- If the lock key is absent, cron sets it with a TTL and proceeds.
|
||||
- TTL ensures the lock is released even if a node crashes.
|
||||
|
||||
Service: `src/cron/services/base-cron.service.ts`
|
||||
|
||||
## RabbitMQ Setup and Behavior
|
||||
|
||||
The allowance system uses RabbitMQ for asynchronous processing. Cron publishes due schedules,
|
||||
and workers consume them.
|
||||
|
||||
### Exchanges and Queues
|
||||
|
||||
Main queue:
|
||||
- `allowance.schedule`
|
||||
|
||||
Retry setup:
|
||||
- Exchange: `allowance.schedule.retry.exchange`
|
||||
- Queue: `allowance.schedule.retry`
|
||||
- Binding key: `allowance.schedule`
|
||||
- TTL: 10 minutes
|
||||
- Dead-letter route: back to `allowance.schedule`
|
||||
|
||||
Dead-letter queue (DLQ):
|
||||
- Exchange: `allowance.schedule.dlq.exchange`
|
||||
- Queue: `allowance.schedule.dlq`
|
||||
- Binding key: `allowance.schedule`
|
||||
|
||||
### Flow Summary
|
||||
|
||||
1. Cron enqueues jobs to `allowance.schedule`.
|
||||
2. Worker consumes jobs from `allowance.schedule`.
|
||||
3. On failure, job is **dead-lettered** to `allowance.schedule.retry`.
|
||||
4. After 10 minutes, it returns to `allowance.schedule`.
|
||||
5. After max retries, worker publishes the job to `allowance.schedule.dlq`.
|
||||
|
||||
### Where it is configured
|
||||
|
||||
- Publisher setup: `src/allowance/services/allowance-queue.service.ts`
|
||||
- Worker consumer: `src/allowance/services/allowance-worker.service.ts`
|
||||
- Queue constants: `src/allowance/constants/allowance-queue.constants.ts`
|
||||
|
||||
## Environment Variables
|
||||
|
||||
- `RABBITMQ_URL` (required for queue/worker)
|
||||
- `ALLOWANCE_QUEUE_NAME` (optional, defaults to `allowance.schedule`)
|
||||
- `ALLOWANCE_MAX_RETRIES` (optional, defaults to 5)
|
||||
- `ALLOWANCE_RETRY_DELAY_MS` (optional, defaults to 10 minutes)
|
||||
|
||||
### Example .env snippet
|
||||
|
||||
```
|
||||
RABBITMQ_URL=amqp://guest:guest@localhost:5672
|
||||
ALLOWANCE_QUEUE_NAME=allowance.schedule
|
||||
ALLOWANCE_MAX_RETRIES=5
|
||||
ALLOWANCE_RETRY_DELAY_MS=600000
|
||||
```
|
||||
|
||||
## Operational Checklist
|
||||
|
||||
- Ensure Redis is running (cron locking).
|
||||
- Ensure RabbitMQ is running (queue + workers).
|
||||
- Start at least one worker process.
|
||||
- Monitor DLQ for failures.
|
||||
|
||||
## Manual Test Checklist
|
||||
|
||||
1. Create schedule:
|
||||
- POST `/guardians/me/allowances/:juniorId`
|
||||
- Valid amount, frequency, status.
|
||||
2. Duplicate schedule:
|
||||
- Expect `ALLOWANCE.ALREADY_EXISTS`.
|
||||
3. Cron enqueue:
|
||||
- Wait for cron interval or manually trigger.
|
||||
- Confirm messages appear in RabbitMQ.
|
||||
4. Worker:
|
||||
- Ensure worker is running.
|
||||
- Verify transfers happen and `allowance_credits` is created.
|
||||
5. Failure paths:
|
||||
- Simulate transfer failure and verify retry queue behavior.
|
||||
- Confirm DLQ after max retries.
|
||||
|
||||
## Operational Notes
|
||||
|
||||
- For large volumes, scale workers horizontally.
|
||||
- Keep cron lightweight; do not perform transfers in cron.
|
||||
- Monitor queue depth and DLQ entries.
|
||||
|
||||
## Known Limitations (Current)
|
||||
|
||||
- Only one schedule per child (guardian+junior unique).
|
||||
- No custom weekdays or complex schedules.
|
||||
- Retry delay is fixed at 10 minutes (can be configurable).
|
||||
|
||||
## File Map (Quick Reference)
|
||||
|
||||
- API:
|
||||
- `src/allowance/controllers/allowance.controller.ts`
|
||||
- `src/allowance/services/allowance.service.ts`
|
||||
- `src/allowance/dtos/request/create-allowance-schedule.request.dto.ts`
|
||||
- `src/allowance/dtos/response/allowance-schedule.response.dto.ts`
|
||||
|
||||
- Cron:
|
||||
- `src/cron/tasks/allowance-schedule.cron.ts`
|
||||
- `src/cron/services/base-cron.service.ts`
|
||||
|
||||
- Queue/Worker:
|
||||
- `src/allowance/services/allowance-queue.service.ts`
|
||||
- `src/allowance/services/allowance-worker.service.ts`
|
||||
- `src/allowance/constants/allowance-queue.constants.ts`
|
||||
|
||||
- Repositories:
|
||||
- `src/allowance/repositories/allowance-schedule.repository.ts`
|
||||
- `src/allowance/repositories/allowance-credit.repository.ts`
|
||||
|
||||
- Entities:
|
||||
- `src/allowance/entities/allowance-schedule.entity.ts`
|
||||
- `src/allowance/entities/allowance-credit.entity.ts`
|
||||
|
||||
22
src/allowance/allowance.module.ts
Normal file
22
src/allowance/allowance.module.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { CardModule } from '~/card/card.module';
|
||||
import { JuniorModule } from '~/junior/junior.module';
|
||||
import { AllowanceController } from './controllers';
|
||||
import { AllowanceCredit, AllowanceSchedule } from './entities';
|
||||
import { AllowanceCreditRepository, AllowanceScheduleRepository } from './repositories';
|
||||
import { AllowanceQueueService, AllowanceService, AllowanceWorkerService } from './services';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([AllowanceSchedule, AllowanceCredit]), JuniorModule, CardModule],
|
||||
controllers: [AllowanceController],
|
||||
providers: [
|
||||
AllowanceService,
|
||||
AllowanceScheduleRepository,
|
||||
AllowanceCreditRepository,
|
||||
AllowanceQueueService,
|
||||
AllowanceWorkerService,
|
||||
],
|
||||
exports: [AllowanceScheduleRepository, AllowanceQueueService, AllowanceCreditRepository],
|
||||
})
|
||||
export class AllowanceModule {}
|
||||
5
src/allowance/constants/allowance-queue.constants.ts
Normal file
5
src/allowance/constants/allowance-queue.constants.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export const ALLOWANCE_QUEUE_NAME = 'allowance.schedule';
|
||||
export const ALLOWANCE_RETRY_QUEUE_NAME = 'allowance.schedule.retry';
|
||||
export const ALLOWANCE_DLQ_NAME = 'allowance.schedule.dlq';
|
||||
export const ALLOWANCE_RETRY_EXCHANGE = 'allowance.schedule.retry.exchange';
|
||||
export const ALLOWANCE_DLQ_EXCHANGE = 'allowance.schedule.dlq.exchange';
|
||||
1
src/allowance/constants/index.ts
Normal file
1
src/allowance/constants/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './allowance-queue.constants';
|
||||
78
src/allowance/controllers/allowance.controller.ts
Normal file
78
src/allowance/controllers/allowance.controller.ts
Normal file
@ -0,0 +1,78 @@
|
||||
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Param, Patch, Post, UseGuards } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { Roles } from '~/auth/enums';
|
||||
import { IJwtPayload } from '~/auth/interfaces';
|
||||
import { AllowedRoles, AuthenticatedUser } from '~/common/decorators';
|
||||
import { AccessTokenGuard, RolesGuard } from '~/common/guards';
|
||||
import { ApiDataResponse, ApiLangRequestHeader } from '~/core/decorators';
|
||||
import { ResponseFactory } from '~/core/utils';
|
||||
import { CreateAllowanceScheduleRequestDto, UpdateAllowanceScheduleRequestDto } from '../dtos/request';
|
||||
import {
|
||||
AllowanceScheduleResponseDto,
|
||||
AllowanceSchedulesListResponseDto,
|
||||
AllowanceSummaryResponseDto,
|
||||
} from '../dtos/response';
|
||||
import { AllowanceService } from '../services';
|
||||
|
||||
@Controller('guardians/me/allowances')
|
||||
@ApiTags('Allowances')
|
||||
@ApiBearerAuth()
|
||||
@ApiLangRequestHeader()
|
||||
@UseGuards(AccessTokenGuard, RolesGuard)
|
||||
@AllowedRoles(Roles.GUARDIAN)
|
||||
export class AllowanceController {
|
||||
constructor(private readonly allowanceService: AllowanceService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'Get all allowance schedules for the authenticated guardian' })
|
||||
@ApiDataResponse(AllowanceSchedulesListResponseDto)
|
||||
async getSchedules(@AuthenticatedUser() { sub }: IJwtPayload) {
|
||||
const { withSchedule, withoutSchedule, monthlyTotal } =
|
||||
await this.allowanceService.getSchedulesByGuardian(sub);
|
||||
return ResponseFactory.data(
|
||||
new AllowanceSchedulesListResponseDto(withSchedule, withoutSchedule, monthlyTotal),
|
||||
);
|
||||
}
|
||||
|
||||
@Get('summary')
|
||||
@ApiOperation({ summary: 'Get allowance summary for home page (lightweight)' })
|
||||
@ApiDataResponse(AllowanceSummaryResponseDto)
|
||||
async getSummary(@AuthenticatedUser() { sub }: IJwtPayload) {
|
||||
const { nextPaymentAt, monthlyTotal } = await this.allowanceService.getSummary(sub);
|
||||
return ResponseFactory.data(new AllowanceSummaryResponseDto(nextPaymentAt, monthlyTotal));
|
||||
}
|
||||
|
||||
@Post(':juniorId')
|
||||
@ApiOperation({ summary: 'Create a new allowance schedule for a junior' })
|
||||
@ApiDataResponse(AllowanceScheduleResponseDto)
|
||||
async createSchedule(
|
||||
@AuthenticatedUser() { sub }: IJwtPayload,
|
||||
@Param('juniorId') juniorId: string,
|
||||
@Body() body: CreateAllowanceScheduleRequestDto,
|
||||
) {
|
||||
const schedule = await this.allowanceService.createSchedule(sub, juniorId, body);
|
||||
return ResponseFactory.data(new AllowanceScheduleResponseDto(schedule));
|
||||
}
|
||||
|
||||
@Patch(':scheduleId')
|
||||
@ApiOperation({ summary: 'Update an existing allowance schedule' })
|
||||
@ApiDataResponse(AllowanceScheduleResponseDto)
|
||||
async updateSchedule(
|
||||
@AuthenticatedUser() { sub }: IJwtPayload,
|
||||
@Param('scheduleId') scheduleId: string,
|
||||
@Body() body: UpdateAllowanceScheduleRequestDto,
|
||||
) {
|
||||
const schedule = await this.allowanceService.updateSchedule(sub, scheduleId, body);
|
||||
return ResponseFactory.data(new AllowanceScheduleResponseDto(schedule));
|
||||
}
|
||||
|
||||
@Delete(':scheduleId')
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
@ApiOperation({ summary: 'Delete an allowance schedule' })
|
||||
async deleteSchedule(
|
||||
@AuthenticatedUser() { sub }: IJwtPayload,
|
||||
@Param('scheduleId') scheduleId: string,
|
||||
) {
|
||||
await this.allowanceService.deleteSchedule(sub, scheduleId);
|
||||
}
|
||||
}
|
||||
1
src/allowance/controllers/index.ts
Normal file
1
src/allowance/controllers/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './allowance.controller';
|
||||
@ -0,0 +1,38 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsEnum, IsNotEmpty, IsNumber, IsPositive } from 'class-validator';
|
||||
import { i18nValidationMessage as i18n } from 'nestjs-i18n';
|
||||
import { AllowanceFrequency, AllowanceScheduleStatus } from '~/allowance/enums';
|
||||
|
||||
export class CreateAllowanceScheduleRequestDto {
|
||||
@ApiProperty({ example: 400 })
|
||||
@IsNotEmpty({
|
||||
message: i18n('validation.IsNotEmpty', { path: 'general', property: 'allowance.amount' }),
|
||||
})
|
||||
@IsNumber(
|
||||
{ maxDecimalPlaces: 2 },
|
||||
{ message: i18n('validation.IsNumber', { path: 'general', property: 'allowance.amount' }) },
|
||||
)
|
||||
@IsPositive({
|
||||
message: i18n('validation.IsPositive', { path: 'general', property: 'allowance.amount' }),
|
||||
})
|
||||
amount!: number;
|
||||
|
||||
@ApiProperty({ enum: AllowanceFrequency, example: AllowanceFrequency.WEEKLY })
|
||||
@IsNotEmpty({
|
||||
message: i18n('validation.IsNotEmpty', { path: 'general', property: 'allowance.frequency' }),
|
||||
})
|
||||
@IsEnum(AllowanceFrequency, {
|
||||
message: i18n('validation.IsEnum', { path: 'general', property: 'allowance.frequency' }),
|
||||
})
|
||||
frequency!: AllowanceFrequency;
|
||||
|
||||
@ApiProperty({ enum: AllowanceScheduleStatus, example: AllowanceScheduleStatus.ON })
|
||||
@IsNotEmpty({
|
||||
message: i18n('validation.IsNotEmpty', { path: 'general', property: 'allowance.status' }),
|
||||
})
|
||||
@IsEnum(AllowanceScheduleStatus, {
|
||||
message: i18n('validation.IsEnum', { path: 'general', property: 'allowance.status' }),
|
||||
})
|
||||
status!: AllowanceScheduleStatus;
|
||||
|
||||
}
|
||||
2
src/allowance/dtos/request/index.ts
Normal file
2
src/allowance/dtos/request/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './create-allowance-schedule.request.dto';
|
||||
export * from './update-allowance-schedule.request.dto';
|
||||
@ -0,0 +1,40 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsEnum, IsNotEmpty, IsNumber, IsOptional, IsPositive } from 'class-validator';
|
||||
import { i18nValidationMessage } from 'nestjs-i18n';
|
||||
import { AllowanceFrequency, AllowanceScheduleStatus } from '~/allowance/enums';
|
||||
|
||||
export class UpdateAllowanceScheduleRequestDto {
|
||||
@ApiPropertyOptional({ example: 150, description: 'Allowance amount' })
|
||||
@IsOptional()
|
||||
@IsNotEmpty({ message: i18nValidationMessage('validation.NOT_EMPTY') })
|
||||
@IsNumber(
|
||||
{ maxDecimalPlaces: 2 },
|
||||
{ message: i18nValidationMessage('validation.INVALID_NUMBER', { field: 'general.amount' }) },
|
||||
)
|
||||
@IsPositive({ message: i18nValidationMessage('validation.MUST_BE_POSITIVE', { field: 'general.amount' }) })
|
||||
amount?: number;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
enum: AllowanceFrequency,
|
||||
example: AllowanceFrequency.WEEKLY,
|
||||
description: 'How often the allowance is paid',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsNotEmpty({ message: i18nValidationMessage('validation.NOT_EMPTY') })
|
||||
@IsEnum(AllowanceFrequency, {
|
||||
message: i18nValidationMessage('validation.INVALID_ENUM', { field: 'general.allowance.frequency' }),
|
||||
})
|
||||
frequency?: AllowanceFrequency;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
enum: AllowanceScheduleStatus,
|
||||
example: AllowanceScheduleStatus.ON,
|
||||
description: 'Whether the schedule is active or paused',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsNotEmpty({ message: i18nValidationMessage('validation.NOT_EMPTY') })
|
||||
@IsEnum(AllowanceScheduleStatus, {
|
||||
message: i18nValidationMessage('validation.INVALID_ENUM', { field: 'general.allowance.status' }),
|
||||
})
|
||||
status?: AllowanceScheduleStatus;
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { AllowanceSchedule } from '~/allowance/entities/allowance-schedule.entity';
|
||||
import { AllowanceFrequency, AllowanceScheduleStatus } from '~/allowance/enums';
|
||||
|
||||
export class AllowanceScheduleResponseDto {
|
||||
@ApiProperty({ example: '123e4567-e89b-12d3-a456-426614174000' })
|
||||
id!: string;
|
||||
|
||||
@ApiProperty({ example: '123e4567-e89b-12d3-a456-426614174000' })
|
||||
guardianId!: string;
|
||||
|
||||
@ApiProperty({ example: '123e4567-e89b-12d3-a456-426614174000' })
|
||||
juniorId!: string;
|
||||
|
||||
@ApiProperty({ example: 400 })
|
||||
amount!: number;
|
||||
|
||||
@ApiProperty({ enum: AllowanceFrequency, example: AllowanceFrequency.WEEKLY })
|
||||
frequency!: AllowanceFrequency;
|
||||
|
||||
@ApiProperty({ enum: AllowanceScheduleStatus, example: AllowanceScheduleStatus.ON })
|
||||
status!: AllowanceScheduleStatus;
|
||||
|
||||
@ApiProperty({ example: '2026-01-01T00:00:00.000Z' })
|
||||
nextRunAt!: Date;
|
||||
|
||||
@ApiProperty({ example: null })
|
||||
lastRunAt!: Date | null;
|
||||
|
||||
@ApiProperty({ example: '2026-01-01T00:00:00.000Z' })
|
||||
createdAt!: Date;
|
||||
|
||||
@ApiProperty({ example: '2026-01-01T00:00:00.000Z' })
|
||||
updatedAt!: Date;
|
||||
|
||||
constructor(data: AllowanceSchedule) {
|
||||
this.id = data.id;
|
||||
this.guardianId = data.guardianId;
|
||||
this.juniorId = data.juniorId;
|
||||
this.amount = Number(data.amount);
|
||||
this.frequency = data.frequency;
|
||||
this.status = data.status;
|
||||
this.nextRunAt = data.nextRunAt;
|
||||
this.lastRunAt = data.lastRunAt;
|
||||
this.createdAt = data.createdAt;
|
||||
this.updatedAt = data.updatedAt;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { AllowanceSchedule } from '~/allowance/entities/allowance-schedule.entity';
|
||||
import { Junior } from '~/junior/entities';
|
||||
import { JuniorWithScheduleDto, JuniorWithoutScheduleDto } from './junior-allowance-info.response.dto';
|
||||
|
||||
export class AllowanceSchedulesListResponseDto {
|
||||
@ApiProperty({
|
||||
type: [JuniorWithScheduleDto],
|
||||
description: 'Children who have an allowance schedule configured',
|
||||
})
|
||||
withSchedule!: JuniorWithScheduleDto[];
|
||||
|
||||
@ApiProperty({
|
||||
type: [JuniorWithoutScheduleDto],
|
||||
description: 'Children who do not have an allowance schedule yet',
|
||||
})
|
||||
withoutSchedule!: JuniorWithoutScheduleDto[];
|
||||
|
||||
@ApiProperty({
|
||||
example: 1600,
|
||||
description: 'Total monthly equivalent amount for all active schedules',
|
||||
})
|
||||
monthlyTotal!: number;
|
||||
|
||||
constructor(
|
||||
juniorsWithSchedule: { junior: Junior; schedule: AllowanceSchedule }[],
|
||||
juniorsWithoutSchedule: Junior[],
|
||||
monthlyTotal: number,
|
||||
) {
|
||||
this.withSchedule = juniorsWithSchedule.map(
|
||||
({ junior, schedule }) => new JuniorWithScheduleDto(junior, schedule),
|
||||
);
|
||||
this.withoutSchedule = juniorsWithoutSchedule.map((j) => new JuniorWithoutScheduleDto(j));
|
||||
this.monthlyTotal = monthlyTotal;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
|
||||
export class AllowanceSummaryResponseDto {
|
||||
@ApiPropertyOptional({
|
||||
example: '2026-02-01T00:00:00.000Z',
|
||||
description: 'The nearest upcoming payment date among all active schedules (null if no active schedules)',
|
||||
nullable: true,
|
||||
})
|
||||
nextPaymentAt!: Date | null;
|
||||
|
||||
@ApiProperty({
|
||||
example: 1600,
|
||||
description: 'Total monthly equivalent amount for all active schedules',
|
||||
})
|
||||
monthlyTotal!: number;
|
||||
|
||||
constructor(nextPaymentAt: Date | null, monthlyTotal: number) {
|
||||
this.nextPaymentAt = nextPaymentAt;
|
||||
this.monthlyTotal = monthlyTotal;
|
||||
}
|
||||
}
|
||||
4
src/allowance/dtos/response/index.ts
Normal file
4
src/allowance/dtos/response/index.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export * from './allowance-schedule.response.dto';
|
||||
export * from './allowance-schedules-list.response.dto';
|
||||
export * from './allowance-summary.response.dto';
|
||||
export * from './junior-allowance-info.response.dto';
|
||||
@ -0,0 +1,89 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { Junior } from '~/junior/entities';
|
||||
import { AllowanceSchedule } from '~/allowance/entities';
|
||||
import { AllowanceFrequency, AllowanceScheduleStatus } from '~/allowance/enums';
|
||||
|
||||
/**
|
||||
* Junior without an allowance schedule - basic info only
|
||||
*/
|
||||
export class JuniorWithoutScheduleDto {
|
||||
@ApiProperty({ example: '123e4567-e89b-12d3-a456-426614174000' })
|
||||
juniorId!: string;
|
||||
|
||||
@ApiProperty({ example: 'Ahmed' })
|
||||
firstName!: string;
|
||||
|
||||
@ApiProperty({ example: 'Al-Khair' })
|
||||
lastName!: string;
|
||||
|
||||
@ApiProperty({ example: 'https://example.com/profile.jpg', nullable: true })
|
||||
profilePictureUrl!: string | null;
|
||||
|
||||
constructor(junior: Junior) {
|
||||
this.juniorId = junior.id;
|
||||
this.firstName = junior.customer?.user?.firstName || '';
|
||||
this.lastName = junior.customer?.user?.lastName || '';
|
||||
this.profilePictureUrl = junior.customer?.user?.profilePicture?.url || null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Junior with their allowance schedule - all data flattened into one object
|
||||
*/
|
||||
export class JuniorWithScheduleDto {
|
||||
// Junior info
|
||||
@ApiProperty({ example: '123e4567-e89b-12d3-a456-426614174000' })
|
||||
juniorId!: string;
|
||||
|
||||
@ApiProperty({ example: 'Ahmed' })
|
||||
firstName!: string;
|
||||
|
||||
@ApiProperty({ example: 'Al-Khair' })
|
||||
lastName!: string;
|
||||
|
||||
@ApiProperty({ example: 'https://example.com/profile.jpg', nullable: true })
|
||||
profilePictureUrl!: string | null;
|
||||
|
||||
// Schedule info
|
||||
@ApiProperty({ example: '123e4567-e89b-12d3-a456-426614174000' })
|
||||
scheduleId!: string;
|
||||
|
||||
@ApiProperty({ example: 100 })
|
||||
amount!: number;
|
||||
|
||||
@ApiProperty({ enum: AllowanceFrequency, example: AllowanceFrequency.WEEKLY })
|
||||
frequency!: AllowanceFrequency;
|
||||
|
||||
@ApiProperty({ enum: AllowanceScheduleStatus, example: AllowanceScheduleStatus.ON })
|
||||
status!: AllowanceScheduleStatus;
|
||||
|
||||
@ApiProperty({ example: '2026-02-05T00:00:00.000Z' })
|
||||
nextRunAt!: Date;
|
||||
|
||||
@ApiProperty({ example: null, nullable: true })
|
||||
lastRunAt!: Date | null;
|
||||
|
||||
@ApiProperty({ example: '2026-01-15T10:30:00.000Z' })
|
||||
createdAt!: Date;
|
||||
|
||||
@ApiProperty({ example: '2026-01-15T10:30:00.000Z' })
|
||||
updatedAt!: Date;
|
||||
|
||||
constructor(junior: Junior, schedule: AllowanceSchedule) {
|
||||
// Junior info
|
||||
this.juniorId = junior.id;
|
||||
this.firstName = junior.customer?.user?.firstName || '';
|
||||
this.lastName = junior.customer?.user?.lastName || '';
|
||||
this.profilePictureUrl = junior.customer?.user?.profilePicture?.url || null;
|
||||
|
||||
// Schedule info
|
||||
this.scheduleId = schedule.id;
|
||||
this.amount = Number(schedule.amount);
|
||||
this.frequency = schedule.frequency;
|
||||
this.status = schedule.status;
|
||||
this.nextRunAt = schedule.nextRunAt;
|
||||
this.lastRunAt = schedule.lastRunAt;
|
||||
this.createdAt = schedule.createdAt;
|
||||
this.updatedAt = schedule.updatedAt;
|
||||
}
|
||||
}
|
||||
42
src/allowance/entities/allowance-credit.entity.ts
Normal file
42
src/allowance/entities/allowance-credit.entity.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import {
|
||||
BaseEntity,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
} from 'typeorm';
|
||||
import { Transaction } from '~/card/entities/transaction.entity';
|
||||
import { AllowanceSchedule } from './allowance-schedule.entity';
|
||||
|
||||
@Entity('allowance_credits')
|
||||
export class AllowanceCredit extends BaseEntity {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id!: string;
|
||||
|
||||
@Column({ type: 'decimal', precision: 12, scale: 2, name: 'amount' })
|
||||
amount!: number;
|
||||
|
||||
@Column({ type: 'timestamp with time zone', name: 'run_at' })
|
||||
runAt!: Date;
|
||||
|
||||
@CreateDateColumn({ type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP', name: 'credited_at' })
|
||||
creditedAt!: Date;
|
||||
|
||||
@Column({ type: 'uuid', name: 'schedule_id' })
|
||||
scheduleId!: string;
|
||||
|
||||
|
||||
@ManyToOne(() => AllowanceSchedule, (schedule) => schedule.credits, { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'schedule_id' })
|
||||
schedule!: AllowanceSchedule;
|
||||
|
||||
@Column({ type: 'uuid', name: 'transaction_id', nullable: true })
|
||||
transactionId!: string | null;
|
||||
|
||||
@ManyToOne(() => Transaction, { onDelete: 'SET NULL', nullable: true })
|
||||
@JoinColumn({ name: 'transaction_id' })
|
||||
transaction!: Transaction | null;
|
||||
|
||||
}
|
||||
60
src/allowance/entities/allowance-schedule.entity.ts
Normal file
60
src/allowance/entities/allowance-schedule.entity.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import {
|
||||
BaseEntity,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
OneToMany,
|
||||
PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
import { Guardian } from '~/guardian/entities/guradian.entity';
|
||||
import { Junior } from '~/junior/entities';
|
||||
import { AllowanceFrequency, AllowanceScheduleStatus } from '../enums';
|
||||
import { AllowanceCredit } from './allowance-credit.entity';
|
||||
|
||||
@Entity('allowance_schedules')
|
||||
export class AllowanceSchedule extends BaseEntity {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id!: string;
|
||||
|
||||
@Column({ type: 'decimal', precision: 12, scale: 2, name: 'amount' })
|
||||
amount!: number;
|
||||
|
||||
@Column({ type: 'varchar', name: 'frequency' })
|
||||
frequency!: AllowanceFrequency;
|
||||
|
||||
@Column({ type: 'varchar', name: 'status', default: AllowanceScheduleStatus.ON })
|
||||
status!: AllowanceScheduleStatus;
|
||||
|
||||
@Column({ type: 'timestamp with time zone', name: 'next_run_at' })
|
||||
nextRunAt!: Date;
|
||||
|
||||
@Column({ type: 'timestamp with time zone', name: 'last_run_at', nullable: true })
|
||||
lastRunAt!: Date | null;
|
||||
|
||||
@CreateDateColumn({ type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP', name: 'created_at' })
|
||||
createdAt!: Date;
|
||||
|
||||
@UpdateDateColumn({ type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP', name: 'updated_at' })
|
||||
updatedAt!: Date;
|
||||
|
||||
@Column({ type: 'uuid', name: 'guardian_id' })
|
||||
guardianId!: string;
|
||||
|
||||
@ManyToOne(() => Guardian, (guardian) => guardian.allowanceSchedules, { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'guardian_id' })
|
||||
guardian!: Guardian;
|
||||
|
||||
@Column({ type: 'uuid', name: 'junior_id' })
|
||||
juniorId!: string;
|
||||
|
||||
@ManyToOne(() => Junior, (junior) => junior.allowanceSchedules, { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'junior_id' })
|
||||
junior!: Junior;
|
||||
|
||||
@OneToMany(() => AllowanceCredit, (credit) => credit.schedule)
|
||||
credits!: AllowanceCredit[];
|
||||
|
||||
}
|
||||
2
src/allowance/entities/index.ts
Normal file
2
src/allowance/entities/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './allowance-credit.entity';
|
||||
export * from './allowance-schedule.entity';
|
||||
5
src/allowance/enums/allowance-frequency.enum.ts
Normal file
5
src/allowance/enums/allowance-frequency.enum.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export enum AllowanceFrequency {
|
||||
DAILY = 'DAILY',
|
||||
WEEKLY = 'WEEKLY',
|
||||
MONTHLY = 'MONTHLY',
|
||||
}
|
||||
4
src/allowance/enums/allowance-schedule-status.enum.ts
Normal file
4
src/allowance/enums/allowance-schedule-status.enum.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export enum AllowanceScheduleStatus {
|
||||
ON = 'ON',
|
||||
OFF = 'OFF',
|
||||
}
|
||||
2
src/allowance/enums/index.ts
Normal file
2
src/allowance/enums/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './allowance-frequency.enum';
|
||||
export * from './allowance-schedule-status.enum';
|
||||
@ -0,0 +1,13 @@
|
||||
import { Junior } from '~/junior/entities';
|
||||
import { AllowanceSchedule } from '../entities/allowance-schedule.entity';
|
||||
|
||||
export interface AllowanceSchedulesGrouped {
|
||||
withSchedule: { junior: Junior; schedule: AllowanceSchedule }[];
|
||||
withoutSchedule: Junior[];
|
||||
monthlyTotal: number;
|
||||
}
|
||||
|
||||
export interface AllowanceSummary {
|
||||
nextPaymentAt: Date | null;
|
||||
monthlyTotal: number;
|
||||
}
|
||||
1
src/allowance/interfaces/index.ts
Normal file
1
src/allowance/interfaces/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './allowance-schedules-grouped.interface';
|
||||
32
src/allowance/repositories/allowance-credit.repository.ts
Normal file
32
src/allowance/repositories/allowance-credit.repository.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { AllowanceCredit } from '~/allowance/entities';
|
||||
|
||||
@Injectable()
|
||||
export class AllowanceCreditRepository {
|
||||
constructor(
|
||||
@InjectRepository(AllowanceCredit)
|
||||
private readonly allowanceCreditRepository: Repository<AllowanceCredit>,
|
||||
) {}
|
||||
|
||||
createCredit(scheduleId: string, amount: number, runAt: Date): Promise<AllowanceCredit> {
|
||||
return this.allowanceCreditRepository.save(
|
||||
this.allowanceCreditRepository.create({
|
||||
scheduleId,
|
||||
amount,
|
||||
runAt,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
findByScheduleAndRunAt(scheduleId: string, runAt: Date): Promise<AllowanceCredit | null> {
|
||||
return this.allowanceCreditRepository.findOne({
|
||||
where: { scheduleId, runAt },
|
||||
});
|
||||
}
|
||||
|
||||
deleteById(id: string): Promise<void> {
|
||||
return this.allowanceCreditRepository.delete({ id }).then(() => undefined);
|
||||
}
|
||||
}
|
||||
96
src/allowance/repositories/allowance-schedule.repository.ts
Normal file
96
src/allowance/repositories/allowance-schedule.repository.ts
Normal file
@ -0,0 +1,96 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { AllowanceSchedule } from '~/allowance/entities/allowance-schedule.entity';
|
||||
import { CreateAllowanceScheduleRequestDto } from '~/allowance/dtos/request';
|
||||
import { AllowanceScheduleStatus } from '~/allowance/enums';
|
||||
|
||||
@Injectable()
|
||||
export class AllowanceScheduleRepository {
|
||||
constructor(
|
||||
@InjectRepository(AllowanceSchedule)
|
||||
private readonly allowanceScheduleRepository: Repository<AllowanceSchedule>,
|
||||
) {}
|
||||
|
||||
findByGuardianAndJunior(guardianId: string, juniorId: string): Promise<AllowanceSchedule | null> {
|
||||
return this.allowanceScheduleRepository.findOne({
|
||||
where: { guardianId, juniorId },
|
||||
});
|
||||
}
|
||||
|
||||
findByGuardianId(guardianId: string): Promise<AllowanceSchedule[]> {
|
||||
return this.allowanceScheduleRepository.find({
|
||||
where: { guardianId },
|
||||
order: { createdAt: 'DESC' },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds only active (ON) schedules for a guardian, ordered by nextRunAt (nearest first)
|
||||
*/
|
||||
findActiveByGuardianId(guardianId: string): Promise<AllowanceSchedule[]> {
|
||||
return this.allowanceScheduleRepository.find({
|
||||
where: { guardianId, status: AllowanceScheduleStatus.ON },
|
||||
order: { nextRunAt: 'ASC' },
|
||||
});
|
||||
}
|
||||
|
||||
createSchedule(guardianId: string, juniorId: string, body: CreateAllowanceScheduleRequestDto, nextRunAt: Date) {
|
||||
return this.allowanceScheduleRepository.save(
|
||||
this.allowanceScheduleRepository.create({
|
||||
guardianId,
|
||||
juniorId,
|
||||
amount: body.amount,
|
||||
frequency: body.frequency,
|
||||
status: body.status,
|
||||
nextRunAt,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
findDueSchedulesBatch(
|
||||
limit: number,
|
||||
cursor?: { nextRunAt: Date; id: string },
|
||||
): Promise<AllowanceSchedule[]> {
|
||||
const query = this.allowanceScheduleRepository
|
||||
.createQueryBuilder('schedule')
|
||||
.where('schedule.status = :status', { status: AllowanceScheduleStatus.ON })
|
||||
.andWhere('schedule.nextRunAt <= :now', { now: new Date() });
|
||||
|
||||
if (cursor) {
|
||||
query.andWhere(
|
||||
'(schedule.nextRunAt > :cursorDate OR (schedule.nextRunAt = :cursorDate AND schedule.id > :cursorId))',
|
||||
{
|
||||
cursorDate: cursor.nextRunAt,
|
||||
cursorId: cursor.id,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return query
|
||||
.orderBy('schedule.nextRunAt', 'ASC')
|
||||
.addOrderBy('schedule.id', 'ASC')
|
||||
.take(limit)
|
||||
.getMany();
|
||||
}
|
||||
|
||||
findById(id: string): Promise<AllowanceSchedule | null> {
|
||||
return this.allowanceScheduleRepository.findOne({ where: { id } });
|
||||
}
|
||||
|
||||
findByIdAndGuardian(id: string, guardianId: string): Promise<AllowanceSchedule | null> {
|
||||
return this.allowanceScheduleRepository.findOne({ where: { id, guardianId } });
|
||||
}
|
||||
|
||||
updateScheduleRun(id: string, lastRunAt: Date, nextRunAt: Date) {
|
||||
return this.allowanceScheduleRepository.update({ id }, { lastRunAt, nextRunAt });
|
||||
}
|
||||
|
||||
async updateSchedule(schedule: AllowanceSchedule): Promise<AllowanceSchedule> {
|
||||
return this.allowanceScheduleRepository.save(schedule);
|
||||
}
|
||||
|
||||
async deleteById(id: string): Promise<void> {
|
||||
await this.allowanceScheduleRepository.delete({ id });
|
||||
}
|
||||
}
|
||||
2
src/allowance/repositories/index.ts
Normal file
2
src/allowance/repositories/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './allowance-credit.repository';
|
||||
export * from './allowance-schedule.repository';
|
||||
90
src/allowance/services/allowance-queue.service.ts
Normal file
90
src/allowance/services/allowance-queue.service.ts
Normal file
@ -0,0 +1,90 @@
|
||||
import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { AmqpConnectionManager, ChannelWrapper, connect } from 'amqp-connection-manager';
|
||||
import {
|
||||
ALLOWANCE_DLQ_EXCHANGE,
|
||||
ALLOWANCE_DLQ_NAME,
|
||||
ALLOWANCE_QUEUE_NAME,
|
||||
ALLOWANCE_RETRY_EXCHANGE,
|
||||
ALLOWANCE_RETRY_QUEUE_NAME,
|
||||
} from '../constants';
|
||||
|
||||
@Injectable()
|
||||
export class AllowanceQueueService implements OnModuleDestroy {
|
||||
private readonly logger = new Logger(AllowanceQueueService.name);
|
||||
private connection?: AmqpConnectionManager;
|
||||
private channel?: ChannelWrapper;
|
||||
private readonly queueName: string;
|
||||
private readonly rabbitUrl?: string;
|
||||
private readonly retryDelayMs: number;
|
||||
|
||||
constructor(private readonly configService: ConfigService) {
|
||||
this.queueName = this.configService.get<string>('ALLOWANCE_QUEUE_NAME') || ALLOWANCE_QUEUE_NAME;
|
||||
this.rabbitUrl = this.configService.get<string>('RABBITMQ_URL');
|
||||
this.retryDelayMs = Number(this.configService.get<string>('ALLOWANCE_RETRY_DELAY_MS') || 10 * 60 * 1000);
|
||||
}
|
||||
|
||||
async enqueueSchedule(scheduleId: string, runAt: Date): Promise<void> {
|
||||
if (!this.rabbitUrl) {
|
||||
this.logger.warn('RABBITMQ_URL is not set; skipping allowance enqueue.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.connection || !this.channel) {
|
||||
this.connection = connect([this.rabbitUrl]);
|
||||
this.connection.on('connect', () => {
|
||||
this.logger.log('RabbitMQ connected (publisher).');
|
||||
});
|
||||
this.connection.on('disconnect', (params) => {
|
||||
this.logger.error(`RabbitMQ disconnected (publisher): ${params?.err?.message || 'unknown error'}`);
|
||||
});
|
||||
this.channel = this.connection.createChannel({
|
||||
setup: async (channel: any) => {
|
||||
await channel.assertExchange(ALLOWANCE_RETRY_EXCHANGE, 'direct', { durable: true });
|
||||
await channel.assertExchange(ALLOWANCE_DLQ_EXCHANGE, 'direct', { durable: true });
|
||||
|
||||
await channel.assertQueue(ALLOWANCE_RETRY_QUEUE_NAME, {
|
||||
durable: true,
|
||||
messageTtl: this.retryDelayMs,
|
||||
deadLetterExchange: '',
|
||||
deadLetterRoutingKey: this.queueName,
|
||||
});
|
||||
|
||||
await channel.assertQueue(ALLOWANCE_DLQ_NAME, {
|
||||
durable: true,
|
||||
});
|
||||
|
||||
await channel.bindQueue(ALLOWANCE_RETRY_QUEUE_NAME, ALLOWANCE_RETRY_EXCHANGE, this.queueName);
|
||||
await channel.bindQueue(ALLOWANCE_DLQ_NAME, ALLOWANCE_DLQ_EXCHANGE, this.queueName);
|
||||
|
||||
await channel.assertQueue(this.queueName, {
|
||||
durable: true,
|
||||
deadLetterExchange: ALLOWANCE_RETRY_EXCHANGE,
|
||||
deadLetterRoutingKey: this.queueName,
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const payload = {
|
||||
scheduleId,
|
||||
runAt: runAt.toISOString(),
|
||||
};
|
||||
|
||||
const messageId = `allowance:${scheduleId}:${runAt.toISOString()}`;
|
||||
const options: any = {
|
||||
persistent: true,
|
||||
messageId,
|
||||
contentType: 'application/json',
|
||||
};
|
||||
|
||||
this.logger.log(`Enqueueing allowance job - scheduleId: ${scheduleId}, runAt: ${runAt.toISOString()}`);
|
||||
await this.channel.sendToQueue(this.queueName, Buffer.from(JSON.stringify(payload)), options);
|
||||
this.logger.log(`Allowance job enqueued successfully - messageId: ${messageId}`);
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
await this.channel?.close();
|
||||
await this.connection?.close();
|
||||
}
|
||||
}
|
||||
212
src/allowance/services/allowance-worker.service.ts
Normal file
212
src/allowance/services/allowance-worker.service.ts
Normal file
@ -0,0 +1,212 @@
|
||||
import { Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { AmqpConnectionManager, ChannelWrapper, connect } from 'amqp-connection-manager';
|
||||
import moment from 'moment';
|
||||
import { CardService } from '~/card/services';
|
||||
import { AllowanceScheduleRepository } from '../repositories/allowance-schedule.repository';
|
||||
import { AllowanceCreditRepository } from '../repositories/allowance-credit.repository';
|
||||
import { ALLOWANCE_DLQ_EXCHANGE, ALLOWANCE_QUEUE_NAME, ALLOWANCE_RETRY_EXCHANGE } from '../constants';
|
||||
import { AllowanceFrequency, AllowanceScheduleStatus } from '../enums';
|
||||
|
||||
type AllowanceQueuePayload = {
|
||||
scheduleId: string;
|
||||
runAt: string;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class AllowanceWorkerService implements OnModuleInit, OnModuleDestroy {
|
||||
private readonly logger = new Logger(AllowanceWorkerService.name);
|
||||
private connection?: AmqpConnectionManager;
|
||||
private channel?: ChannelWrapper;
|
||||
private readonly queueName: string;
|
||||
private readonly rabbitUrl?: string;
|
||||
private readonly maxRetries: number;
|
||||
private readonly isTestMode: boolean;
|
||||
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly allowanceScheduleRepository: AllowanceScheduleRepository,
|
||||
private readonly allowanceCreditRepository: AllowanceCreditRepository,
|
||||
private readonly cardService: CardService,
|
||||
) {
|
||||
this.queueName = this.configService.get<string>('ALLOWANCE_QUEUE_NAME') || ALLOWANCE_QUEUE_NAME;
|
||||
this.rabbitUrl = this.configService.get<string>('RABBITMQ_URL');
|
||||
this.maxRetries = Number(this.configService.get<string>('ALLOWANCE_MAX_RETRIES') || 5);
|
||||
this.isTestMode = this.configService.get<string>('ALLOWANCE_TEST_MODE') === 'true';
|
||||
if (this.isTestMode) {
|
||||
this.logger.warn('ALLOWANCE_TEST_MODE is enabled - using short intervals (5/10/15 min)');
|
||||
}
|
||||
}
|
||||
|
||||
async onModuleInit() {
|
||||
if (!this.rabbitUrl) {
|
||||
this.logger.warn('RABBITMQ_URL is not set; allowance worker is disabled.');
|
||||
return;
|
||||
}
|
||||
|
||||
this.connection = connect([this.rabbitUrl]);
|
||||
this.connection.on('connect', () => {
|
||||
this.logger.log('RabbitMQ connected (worker).');
|
||||
});
|
||||
this.connection.on('disconnect', (params) => {
|
||||
this.logger.error(`RabbitMQ disconnected (worker): ${params?.err?.message || 'unknown error'}`);
|
||||
});
|
||||
this.channel = this.connection.createChannel({
|
||||
setup: async (channel: any) => {
|
||||
await channel.assertQueue(this.queueName, {
|
||||
durable: true,
|
||||
deadLetterExchange: ALLOWANCE_RETRY_EXCHANGE,
|
||||
deadLetterRoutingKey: this.queueName,
|
||||
});
|
||||
await channel.prefetch(10);
|
||||
await channel.consume(this.queueName, (msg: any) => this.handleMessage(channel, msg), {
|
||||
noAck: false,
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
await this.channel?.close();
|
||||
await this.connection?.close();
|
||||
}
|
||||
|
||||
private async handleMessage(channel: any, msg: any) {
|
||||
if (!msg) {
|
||||
return;
|
||||
}
|
||||
|
||||
let payload: AllowanceQueuePayload;
|
||||
try {
|
||||
payload = JSON.parse(msg.content.toString()) as AllowanceQueuePayload;
|
||||
} catch (error) {
|
||||
const stack = error instanceof Error ? error.stack : undefined;
|
||||
this.logger.error('Invalid allowance queue payload', stack || error);
|
||||
channel.ack(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await this.processAllowanceJob(payload);
|
||||
channel.ack(msg);
|
||||
} catch (error) {
|
||||
const stack = error instanceof Error ? error.stack : undefined;
|
||||
this.logger.error(`Allowance job failed for schedule ${payload.scheduleId}`, stack || error);
|
||||
const retryCount = this.getRetryCount(msg);
|
||||
if (retryCount >= this.maxRetries) {
|
||||
this.logger.error(`Allowance job exceeded max retries (${this.maxRetries}). Sending to DLQ.`);
|
||||
channel.publish(ALLOWANCE_DLQ_EXCHANGE, this.queueName, msg.content, {
|
||||
contentType: msg.properties.contentType,
|
||||
messageId: msg.properties.messageId,
|
||||
headers: msg.properties.headers,
|
||||
persistent: true,
|
||||
});
|
||||
channel.ack(msg);
|
||||
return;
|
||||
}
|
||||
channel.nack(msg, false, false);
|
||||
}
|
||||
}
|
||||
|
||||
private async processAllowanceJob(payload: AllowanceQueuePayload): Promise<void> {
|
||||
const runAt = new Date(payload.runAt);
|
||||
if (!payload.scheduleId || Number.isNaN(runAt.getTime())) {
|
||||
this.logger.warn(`Invalid payload - scheduleId: ${payload.scheduleId}, runAt: ${payload.runAt}`);
|
||||
return;
|
||||
}
|
||||
|
||||
this.logger.log(`Processing allowance job - scheduleId: ${payload.scheduleId}, runAt: ${runAt.toISOString()}`);
|
||||
|
||||
const schedule = await this.allowanceScheduleRepository.findById(payload.scheduleId);
|
||||
if (!schedule) {
|
||||
this.logger.warn(`Schedule not found: ${payload.scheduleId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
this.logger.debug(`Schedule found - juniorId: ${schedule.juniorId}, amount: ${schedule.amount}, status: ${schedule.status}, nextRunAt: ${schedule.nextRunAt}`);
|
||||
|
||||
if (schedule.status !== AllowanceScheduleStatus.ON) {
|
||||
this.logger.warn(`Schedule ${payload.scheduleId} is not ON (status: ${schedule.status}). Skipping.`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (schedule.nextRunAt > runAt) {
|
||||
this.logger.warn(`Schedule ${payload.scheduleId} nextRunAt (${schedule.nextRunAt}) > runAt (${runAt}). Skipping.`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert amount from decimal string to number
|
||||
const amount = Number(schedule.amount);
|
||||
if (isNaN(amount) || amount <= 0) {
|
||||
this.logger.error(`Invalid amount for schedule ${payload.scheduleId}: ${schedule.amount}`);
|
||||
return;
|
||||
}
|
||||
|
||||
this.logger.log(`Creating allowance credit - scheduleId: ${payload.scheduleId}, amount: ${amount}`);
|
||||
|
||||
let credit = null;
|
||||
try {
|
||||
credit = await this.allowanceCreditRepository.createCredit(schedule.id, amount, runAt);
|
||||
this.logger.log(`Credit created: ${credit.id}`);
|
||||
} catch (error: any) {
|
||||
if (error?.code === '23505') {
|
||||
this.logger.warn(`Credit already exists for schedule ${payload.scheduleId} at ${runAt.toISOString()} (idempotency check)`);
|
||||
return;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
try {
|
||||
this.logger.log(`Transferring ${amount} to junior ${schedule.juniorId}`);
|
||||
await this.cardService.transferToChild(schedule.juniorId, amount);
|
||||
this.logger.log(`Transfer successful for junior ${schedule.juniorId}`);
|
||||
|
||||
const nextRunAt = this.computeNextRunAt(schedule.frequency);
|
||||
await this.allowanceScheduleRepository.updateScheduleRun(schedule.id, runAt, nextRunAt);
|
||||
this.logger.log(`Schedule ${payload.scheduleId} updated - lastRunAt: ${runAt.toISOString()}, nextRunAt: ${nextRunAt.toISOString()}`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Transfer failed for schedule ${payload.scheduleId}: ${error instanceof Error ? error.message : error}`);
|
||||
if (credit) {
|
||||
await this.allowanceCreditRepository.deleteById(credit.id);
|
||||
this.logger.log(`Credit ${credit.id} deleted due to transfer failure`);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private computeNextRunAt(frequency: AllowanceFrequency): Date {
|
||||
const base = moment();
|
||||
|
||||
if (this.isTestMode) {
|
||||
// Test mode: DAILY=5min, WEEKLY=10min, MONTHLY=15min
|
||||
switch (frequency) {
|
||||
case AllowanceFrequency.DAILY:
|
||||
return base.add(5, 'minutes').toDate();
|
||||
case AllowanceFrequency.WEEKLY:
|
||||
return base.add(10, 'minutes').toDate();
|
||||
case AllowanceFrequency.MONTHLY:
|
||||
return base.add(15, 'minutes').toDate();
|
||||
default:
|
||||
return base.toDate();
|
||||
}
|
||||
}
|
||||
|
||||
// Production mode: real intervals
|
||||
switch (frequency) {
|
||||
case AllowanceFrequency.DAILY:
|
||||
return base.add(1, 'day').toDate();
|
||||
case AllowanceFrequency.WEEKLY:
|
||||
return base.add(1, 'week').toDate();
|
||||
case AllowanceFrequency.MONTHLY:
|
||||
return base.add(1, 'month').toDate();
|
||||
default:
|
||||
return base.toDate();
|
||||
}
|
||||
}
|
||||
|
||||
private getRetryCount(msg: any): number {
|
||||
const deaths = (msg.properties.headers?.['x-death'] as Array<{ count?: number }> | undefined) || [];
|
||||
const retryDeath = deaths.find((death) => death?.count != null);
|
||||
return retryDeath?.count ?? 0;
|
||||
}
|
||||
}
|
||||
213
src/allowance/services/allowance.service.ts
Normal file
213
src/allowance/services/allowance.service.ts
Normal file
@ -0,0 +1,213 @@
|
||||
import { BadRequestException, Injectable, Logger, NotFoundException } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import moment from 'moment';
|
||||
import { Junior } from '~/junior/entities';
|
||||
import { JuniorService } from '~/junior/services';
|
||||
import { CreateAllowanceScheduleRequestDto, UpdateAllowanceScheduleRequestDto } from '../dtos/request';
|
||||
import { AllowanceSchedule } from '../entities/allowance-schedule.entity';
|
||||
import { AllowanceFrequency, AllowanceScheduleStatus } from '../enums';
|
||||
import { AllowanceSchedulesGrouped, AllowanceSummary } from '../interfaces';
|
||||
import { AllowanceScheduleRepository } from '../repositories';
|
||||
|
||||
@Injectable()
|
||||
export class AllowanceService {
|
||||
private readonly logger = new Logger(AllowanceService.name);
|
||||
private readonly isTestMode: boolean;
|
||||
|
||||
constructor(
|
||||
private readonly allowanceScheduleRepository: AllowanceScheduleRepository,
|
||||
private readonly juniorService: JuniorService,
|
||||
private readonly configService: ConfigService,
|
||||
) {
|
||||
this.isTestMode = this.configService.get<string>('ALLOWANCE_TEST_MODE') === 'true';
|
||||
if (this.isTestMode) {
|
||||
this.logger.warn('ALLOWANCE_TEST_MODE is enabled - using short intervals (5/10/15 min)');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all allowance schedules for a guardian, grouped by juniors with and without schedules.
|
||||
*/
|
||||
async getSchedulesByGuardian(guardianId: string): Promise<AllowanceSchedulesGrouped> {
|
||||
// Fetch all juniors for this guardian (with pagination workaround - large size)
|
||||
const [juniors] = await this.juniorService.findJuniorsByGuardianId(guardianId, {
|
||||
page: 1,
|
||||
size: 1000, // Assuming no guardian has more than 1000 children
|
||||
});
|
||||
|
||||
// Fetch all schedules for this guardian
|
||||
const schedules = await this.allowanceScheduleRepository.findByGuardianId(guardianId);
|
||||
|
||||
// Create a map of juniorId -> schedule for quick lookup
|
||||
const scheduleMap = new Map<string, AllowanceSchedule>();
|
||||
for (const schedule of schedules) {
|
||||
scheduleMap.set(schedule.juniorId, schedule);
|
||||
}
|
||||
|
||||
// Separate juniors into two groups
|
||||
const withSchedule: { junior: Junior; schedule: AllowanceSchedule }[] = [];
|
||||
const withoutSchedule: Junior[] = [];
|
||||
|
||||
for (const junior of juniors) {
|
||||
const schedule = scheduleMap.get(junior.id);
|
||||
if (schedule) {
|
||||
withSchedule.push({ junior, schedule });
|
||||
} else {
|
||||
withoutSchedule.push(junior);
|
||||
}
|
||||
}
|
||||
|
||||
const monthlyTotal = this.calculateMonthlyTotal(schedules);
|
||||
|
||||
return { withSchedule, withoutSchedule, monthlyTotal };
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the monthly equivalent total for all active schedules.
|
||||
* - DAILY: amount * 30
|
||||
* - WEEKLY: amount * 4
|
||||
* - MONTHLY: amount * 1
|
||||
*/
|
||||
private calculateMonthlyTotal(schedules: AllowanceSchedule[]): number {
|
||||
return schedules
|
||||
.filter((s) => s.status === AllowanceScheduleStatus.ON)
|
||||
.reduce((total, schedule) => {
|
||||
const amount = Number(schedule.amount);
|
||||
switch (schedule.frequency) {
|
||||
case AllowanceFrequency.DAILY:
|
||||
return total + amount * 30;
|
||||
case AllowanceFrequency.WEEKLY:
|
||||
return total + amount * 4;
|
||||
case AllowanceFrequency.MONTHLY:
|
||||
return total + amount;
|
||||
default:
|
||||
return total;
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
|
||||
async createSchedule(
|
||||
guardianId: string,
|
||||
juniorId: string,
|
||||
body: CreateAllowanceScheduleRequestDto,
|
||||
): Promise<AllowanceSchedule> {
|
||||
const doesBelong = await this.juniorService.doesJuniorBelongToGuardian(guardianId, juniorId);
|
||||
if (!doesBelong) {
|
||||
this.logger.error(`Junior ${juniorId} does not belong to guardian ${guardianId}`);
|
||||
throw new BadRequestException('JUNIOR.NOT_BELONG_TO_GUARDIAN');
|
||||
}
|
||||
|
||||
const existingSchedule = await this.allowanceScheduleRepository.findByGuardianAndJunior(guardianId, juniorId);
|
||||
if (existingSchedule) {
|
||||
throw new BadRequestException('ALLOWANCE.ALREADY_EXISTS');
|
||||
}
|
||||
|
||||
const nextRunAt = this.computeNextRunAt(body.frequency, body.status);
|
||||
return this.allowanceScheduleRepository.createSchedule(guardianId, juniorId, body, nextRunAt);
|
||||
}
|
||||
|
||||
private computeNextRunAt(frequency: AllowanceFrequency, status: AllowanceScheduleStatus): Date {
|
||||
const base = moment();
|
||||
if (status === AllowanceScheduleStatus.OFF) {
|
||||
return base.toDate();
|
||||
}
|
||||
|
||||
if (this.isTestMode) {
|
||||
// Test mode: DAILY=5min, WEEKLY=10min, MONTHLY=15min
|
||||
switch (frequency) {
|
||||
case AllowanceFrequency.DAILY:
|
||||
return base.add(5, 'minutes').toDate();
|
||||
case AllowanceFrequency.WEEKLY:
|
||||
return base.add(10, 'minutes').toDate();
|
||||
case AllowanceFrequency.MONTHLY:
|
||||
return base.add(15, 'minutes').toDate();
|
||||
default:
|
||||
return base.toDate();
|
||||
}
|
||||
}
|
||||
|
||||
// Production mode: real intervals
|
||||
switch (frequency) {
|
||||
case AllowanceFrequency.DAILY:
|
||||
return base.add(1, 'day').toDate();
|
||||
case AllowanceFrequency.WEEKLY:
|
||||
return base.add(1, 'week').toDate();
|
||||
case AllowanceFrequency.MONTHLY:
|
||||
return base.add(1, 'month').toDate();
|
||||
default:
|
||||
return base.toDate();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates an existing allowance schedule.
|
||||
* Recalculates nextRunAt if frequency or status changes.
|
||||
*/
|
||||
async updateSchedule(
|
||||
guardianId: string,
|
||||
scheduleId: string,
|
||||
body: UpdateAllowanceScheduleRequestDto,
|
||||
): Promise<AllowanceSchedule> {
|
||||
const schedule = await this.allowanceScheduleRepository.findByIdAndGuardian(scheduleId, guardianId);
|
||||
|
||||
if (!schedule) {
|
||||
this.logger.error(`Schedule ${scheduleId} not found for guardian ${guardianId}`);
|
||||
throw new NotFoundException('ALLOWANCE.NOT_FOUND');
|
||||
}
|
||||
|
||||
// Check if frequency or status is changing (need to recalculate nextRunAt)
|
||||
const frequencyChanged = body.frequency && body.frequency !== schedule.frequency;
|
||||
const statusChanged = body.status && body.status !== schedule.status;
|
||||
|
||||
// Update fields if provided
|
||||
if (body.amount !== undefined) {
|
||||
schedule.amount = body.amount;
|
||||
}
|
||||
if (body.frequency !== undefined) {
|
||||
schedule.frequency = body.frequency;
|
||||
}
|
||||
if (body.status !== undefined) {
|
||||
schedule.status = body.status;
|
||||
}
|
||||
|
||||
// Recalculate nextRunAt if frequency or status changed
|
||||
if (frequencyChanged || statusChanged) {
|
||||
schedule.nextRunAt = this.computeNextRunAt(schedule.frequency, schedule.status);
|
||||
}
|
||||
|
||||
this.logger.log(`Updating schedule ${scheduleId} for guardian ${guardianId}`);
|
||||
return this.allowanceScheduleRepository.updateSchedule(schedule);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a lightweight summary of allowances for the home page.
|
||||
* Only fetches active schedules for efficiency.
|
||||
*/
|
||||
async getSummary(guardianId: string): Promise<AllowanceSummary> {
|
||||
// Only fetch active schedules, ordered by nextRunAt (nearest first)
|
||||
const activeSchedules = await this.allowanceScheduleRepository.findActiveByGuardianId(guardianId);
|
||||
|
||||
// The first one is the nearest (already sorted by nextRunAt ASC)
|
||||
const nextPaymentAt = activeSchedules.length > 0 ? activeSchedules[0].nextRunAt : null;
|
||||
|
||||
// Calculate monthly total from active schedules
|
||||
const monthlyTotal = this.calculateMonthlyTotal(activeSchedules);
|
||||
|
||||
return { nextPaymentAt, monthlyTotal };
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes an allowance schedule.
|
||||
*/
|
||||
async deleteSchedule(guardianId: string, scheduleId: string): Promise<void> {
|
||||
const schedule = await this.allowanceScheduleRepository.findByIdAndGuardian(scheduleId, guardianId);
|
||||
|
||||
if (!schedule) {
|
||||
this.logger.error(`Schedule ${scheduleId} not found for guardian ${guardianId}`);
|
||||
throw new NotFoundException('ALLOWANCE.NOT_FOUND');
|
||||
}
|
||||
|
||||
this.logger.log(`Deleting schedule ${scheduleId} for guardian ${guardianId}`);
|
||||
await this.allowanceScheduleRepository.deleteById(scheduleId);
|
||||
}
|
||||
}
|
||||
3
src/allowance/services/index.ts
Normal file
3
src/allowance/services/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './allowance-queue.service';
|
||||
export * from './allowance.service';
|
||||
export * from './allowance-worker.service';
|
||||
@ -9,6 +9,7 @@ import { LoggerModule } from 'nestjs-pino';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { addTransactionalDataSource } from 'typeorm-transactional';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import { AllowanceModule } from './allowance/allowance.module';
|
||||
import { CardModule } from './card/card.module';
|
||||
import { CacheModule } from './common/modules/cache/cache.module';
|
||||
import { LookupModule } from './common/modules/lookup/lookup.module';
|
||||
@ -58,6 +59,7 @@ import { MoneyRequestModule } from './money-request/money-request.module';
|
||||
ScheduleModule.forRoot(),
|
||||
// App modules
|
||||
AuthModule,
|
||||
AllowanceModule,
|
||||
UserModule,
|
||||
|
||||
CustomerModule,
|
||||
|
||||
@ -23,4 +23,13 @@ export class JuniorLoginRequestDto {
|
||||
@IsOptional()
|
||||
@IsString({ message: i18n('validation.IsString', { path: 'general', property: 'auth.fcmToken' }) })
|
||||
fcmToken?: string;
|
||||
|
||||
@ApiProperty({
|
||||
example: 'Asia/Riyadh',
|
||||
description: 'Device timezone (auto-detected from device OS)',
|
||||
required: false,
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString({ message: i18n('validation.IsString', { path: 'general', property: 'auth.timezone' }) })
|
||||
timezone?: string;
|
||||
}
|
||||
|
||||
@ -35,4 +35,13 @@ export class LoginRequestDto {
|
||||
@IsOptional()
|
||||
@IsString({ message: i18n('validation.IsString', { path: 'general', property: 'auth.fcmToken' }) })
|
||||
fcmToken?: string;
|
||||
|
||||
@ApiProperty({
|
||||
example: 'Asia/Riyadh',
|
||||
description: 'Device timezone (auto-detected from device OS)',
|
||||
required: false,
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString({ message: i18n('validation.IsString', { path: 'general', property: 'auth.timezone' }) })
|
||||
timezone?: string;
|
||||
}
|
||||
|
||||
@ -115,4 +115,13 @@ export class VerifyUserRequestDto {
|
||||
@IsOptional()
|
||||
@IsString({ message: i18n('validation.IsString', { path: 'general', property: 'auth.fcmToken' }) })
|
||||
fcmToken?: string;
|
||||
|
||||
@ApiProperty({
|
||||
example: 'Asia/Riyadh',
|
||||
description: 'Device timezone (auto-detected from device OS)',
|
||||
required: false,
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString({ message: i18n('validation.IsString', { path: 'general', property: 'auth.timezone' }) })
|
||||
timezone?: string;
|
||||
}
|
||||
|
||||
@ -87,9 +87,9 @@ export class AuthService {
|
||||
const tokens = await this.generateAuthToken(user);
|
||||
this.logger.log(`User with phone number ${user.fullPhoneNumber} verified successfully`);
|
||||
|
||||
// Register/update device with FCM token if provided
|
||||
// Register/update device with FCM token and timezone if provided
|
||||
if (verifyUserDto.fcmToken && verifyUserDto.deviceId) {
|
||||
await this.registerDeviceToken(user.id, verifyUserDto.deviceId, verifyUserDto.fcmToken);
|
||||
await this.registerDeviceToken(user.id, verifyUserDto.deviceId, verifyUserDto.fcmToken, verifyUserDto.timezone);
|
||||
}
|
||||
|
||||
return [tokens, user];
|
||||
@ -278,9 +278,9 @@ export class AuthService {
|
||||
const tokens = await this.generateAuthToken(user);
|
||||
this.logger.log(`Password validated successfully for user`);
|
||||
|
||||
// Register/update device with FCM token if provided
|
||||
// Register/update device with FCM token and timezone if provided
|
||||
if (loginDto.fcmToken && loginDto.deviceId) {
|
||||
await this.registerDeviceToken(user.id, loginDto.deviceId, loginDto.fcmToken);
|
||||
await this.registerDeviceToken(user.id, loginDto.deviceId, loginDto.fcmToken, loginDto.timezone);
|
||||
}
|
||||
|
||||
return [tokens, user];
|
||||
@ -304,22 +304,22 @@ export class AuthService {
|
||||
const tokens = await this.generateAuthToken(user);
|
||||
this.logger.log(`Password validated successfully for user`);
|
||||
|
||||
// Register/update device with FCM token if provided
|
||||
// Register/update device with FCM token and timezone if provided
|
||||
if (juniorLoginDto.fcmToken && juniorLoginDto.deviceId) {
|
||||
await this.registerDeviceToken(user.id, juniorLoginDto.deviceId, juniorLoginDto.fcmToken);
|
||||
await this.registerDeviceToken(user.id, juniorLoginDto.deviceId, juniorLoginDto.fcmToken, juniorLoginDto.timezone);
|
||||
}
|
||||
|
||||
return [tokens, user];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register or update device with FCM token
|
||||
* Register or update device with FCM token and timezone
|
||||
* This method handles:
|
||||
* 1. Device already exists for this user → Update FCM token
|
||||
* 1. Device already exists for this user → Update FCM token and timezone
|
||||
* 2. Device exists for different user → Transfer device to new user
|
||||
* 3. Device doesn't exist → Create new device
|
||||
*/
|
||||
private async registerDeviceToken(userId: string, deviceId: string, fcmToken: string): Promise<void> {
|
||||
private async registerDeviceToken(userId: string, deviceId: string, fcmToken: string, timezone?: string): Promise<void> {
|
||||
try {
|
||||
this.logger.log(`Registering/updating device ${deviceId} with FCM token for user ${userId}`);
|
||||
|
||||
@ -327,13 +327,14 @@ export class AuthService {
|
||||
const existingDeviceForUser = await this.deviceService.findUserDeviceById(deviceId, userId);
|
||||
|
||||
if (existingDeviceForUser) {
|
||||
// Device exists for this user → Update FCM token and last access time
|
||||
// Device exists for this user → Update FCM token, timezone, and last access time
|
||||
await this.deviceService.updateDevice(deviceId, {
|
||||
fcmToken,
|
||||
userId,
|
||||
timezone, // Update timezone if provided
|
||||
lastAccessOn: new Date(),
|
||||
});
|
||||
this.logger.log(`Device ${deviceId} updated with new FCM token for user ${userId}`);
|
||||
this.logger.log(`Device ${deviceId} updated with new FCM token and timezone for user ${userId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -348,6 +349,7 @@ export class AuthService {
|
||||
await this.deviceService.updateDevice(deviceId, {
|
||||
userId,
|
||||
fcmToken,
|
||||
timezone, // Update timezone if provided
|
||||
lastAccessOn: new Date(),
|
||||
});
|
||||
this.logger.log(`Device ${deviceId} transferred from user ${existingDevice.userId} to user ${userId}`);
|
||||
@ -359,6 +361,7 @@ export class AuthService {
|
||||
deviceId,
|
||||
userId,
|
||||
fcmToken,
|
||||
timezone, // Store timezone if provided
|
||||
lastAccessOn: new Date(),
|
||||
});
|
||||
this.logger.log(`New device ${deviceId} registered with FCM token for user ${userId}`);
|
||||
|
||||
@ -27,7 +27,7 @@ import { TransactionService } from './services/transaction.service';
|
||||
AccountService,
|
||||
AccountRepository,
|
||||
],
|
||||
exports: [CardService, TransactionService],
|
||||
exports: [CardService, TransactionService, AccountService],
|
||||
controllers: [CardsController],
|
||||
})
|
||||
export class CardModule {}
|
||||
|
||||
@ -92,7 +92,9 @@ export class TransactionRepository {
|
||||
return this.transactionRepository
|
||||
.createQueryBuilder('transaction')
|
||||
.innerJoinAndSelect('transaction.card', 'card')
|
||||
.where('card.customerId = :juniorId', { juniorId })
|
||||
.innerJoin('card.customer', 'customer')
|
||||
.innerJoin('customer.junior', 'junior')
|
||||
.where('junior.id = :juniorId', { juniorId })
|
||||
.andWhere('transaction.transactionScope = :scope', { scope: TransactionScope.CARD })
|
||||
.andWhere('transaction.transactionType = :type', { type: TransactionType.EXTERNAL })
|
||||
.andWhere('transaction.transactionDate BETWEEN :startDate AND :endDate', { startDate, endDate })
|
||||
@ -153,7 +155,9 @@ export class TransactionRepository {
|
||||
.createQueryBuilder('tx')
|
||||
.innerJoinAndSelect('tx.card', 'card')
|
||||
.innerJoinAndSelect('card.account', 'account')
|
||||
.where('card.customerId = :juniorId', { juniorId })
|
||||
.innerJoin('card.customer', 'customer')
|
||||
.innerJoin('customer.junior', 'junior')
|
||||
.where('junior.id = :juniorId', { juniorId })
|
||||
.andWhere('tx.transactionScope = :scope', { scope: TransactionScope.CARD })
|
||||
.andWhere('tx.transactionType = :type', { type: TransactionType.INTERNAL })
|
||||
.orderBy('tx.transactionDate', 'DESC')
|
||||
@ -166,7 +170,9 @@ export class TransactionRepository {
|
||||
return this.transactionRepository
|
||||
.createQueryBuilder('tx')
|
||||
.innerJoin('tx.card', 'card')
|
||||
.where('card.customerId = :juniorId', { juniorId })
|
||||
.innerJoin('card.customer', 'customer')
|
||||
.innerJoin('customer.junior', 'junior')
|
||||
.where('junior.id = :juniorId', { juniorId })
|
||||
.andWhere('tx.transactionScope = :scope', { scope: TransactionScope.CARD })
|
||||
.andWhere('tx.transactionType = :type', { type: TransactionType.INTERNAL })
|
||||
.getCount();
|
||||
@ -176,8 +182,10 @@ export class TransactionRepository {
|
||||
return this.transactionRepository
|
||||
.createQueryBuilder('tx')
|
||||
.innerJoinAndSelect('tx.card', 'card')
|
||||
.innerJoin('card.customer', 'customer')
|
||||
.innerJoin('customer.junior', 'junior')
|
||||
.where('tx.id = :transactionId', { transactionId })
|
||||
.andWhere('card.customerId = :juniorId', { juniorId })
|
||||
.andWhere('junior.id = :juniorId', { juniorId })
|
||||
.getOne();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
import { BadRequestException, forwardRef, Inject, Injectable, Logger } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import Decimal from 'decimal.js';
|
||||
import { Transactional } from 'typeorm-transactional';
|
||||
import { NOTIFICATION_EVENTS } from '~/common/modules/notification/constants/event-names.constant';
|
||||
import { ICardBlockedEvent, ICardCreatedEvent } from '~/common/modules/notification/interfaces/notification-events.interface';
|
||||
import { AccountCardStatusChangedWebhookRequest } from '~/common/modules/neoleap/dtos/requests';
|
||||
import { NeoLeapService } from '~/common/modules/neoleap/services';
|
||||
import { Customer } from '~/customer/entities';
|
||||
@ -8,7 +11,7 @@ import { KycStatus } from '~/customer/enums';
|
||||
import { CustomerService } from '~/customer/services';
|
||||
import { OciService } from '~/document/services';
|
||||
import { Card } from '../entities';
|
||||
import { CardColors } from '../enums';
|
||||
import { CardColors, CardStatus } from '../enums';
|
||||
import { CardStatusMapper } from '../mappers/card-status.mapper';
|
||||
import { CardRepository } from '../repositories';
|
||||
import { AccountService } from './account.service';
|
||||
@ -24,6 +27,7 @@ export class CardService {
|
||||
@Inject(forwardRef(() => TransactionService)) private readonly transactionService: TransactionService,
|
||||
@Inject(forwardRef(() => NeoLeapService)) private readonly neoleapService: NeoLeapService,
|
||||
@Inject(forwardRef(() => CustomerService)) private readonly customerService: CustomerService,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
) {}
|
||||
|
||||
@Transactional()
|
||||
@ -58,7 +62,16 @@ export class CardService {
|
||||
const account = await this.accountService.createAccount(data);
|
||||
const createdCard = await this.cardRepository.createCard(customerId, account.id, data);
|
||||
|
||||
return this.getCardById(createdCard.id);
|
||||
const cardWithRelations = await this.getCardById(createdCard.id);
|
||||
|
||||
const event: ICardCreatedEvent = {
|
||||
card: cardWithRelations,
|
||||
timestamp: new Date(),
|
||||
};
|
||||
this.eventEmitter.emit(NOTIFICATION_EVENTS.CARD_CREATED, event);
|
||||
this.logger.log(`Emitted CARD_CREATED event for card ${cardWithRelations.id}`);
|
||||
|
||||
return cardWithRelations;
|
||||
}
|
||||
|
||||
async getChildCards(guardianId: string): Promise<Card[]> {
|
||||
@ -77,7 +90,16 @@ export class CardService {
|
||||
parentCustomer.id,
|
||||
);
|
||||
|
||||
return this.getCardById(createdCard.id);
|
||||
const cardWithRelations = await this.getCardById(createdCard.id);
|
||||
|
||||
const event: ICardCreatedEvent = {
|
||||
card: cardWithRelations,
|
||||
timestamp: new Date(),
|
||||
};
|
||||
this.eventEmitter.emit(NOTIFICATION_EVENTS.CARD_CREATED, event);
|
||||
this.logger.log(`Emitted CARD_CREATED event for child card ${cardWithRelations.id}`);
|
||||
|
||||
return cardWithRelations;
|
||||
}
|
||||
|
||||
async getCardByChildId(guardianId: string, childId: string): Promise<Card> {
|
||||
@ -128,9 +150,24 @@ export class CardService {
|
||||
|
||||
async updateCardStatus(body: AccountCardStatusChangedWebhookRequest) {
|
||||
const card = await this.getCardByVpan(body.cardId);
|
||||
const previousStatus = card.status;
|
||||
const { description, status } = CardStatusMapper[body.newStatus] || CardStatusMapper['99'];
|
||||
|
||||
return this.cardRepository.updateCardStatus(card.id, status, description);
|
||||
await this.cardRepository.updateCardStatus(card.id, status, description);
|
||||
|
||||
if (status === CardStatus.BLOCKED) {
|
||||
const updatedCard = await this.getCardById(card.id);
|
||||
const event: ICardBlockedEvent = {
|
||||
card: updatedCard,
|
||||
previousStatus,
|
||||
blockReason: description,
|
||||
timestamp: new Date(),
|
||||
};
|
||||
this.eventEmitter.emit(NOTIFICATION_EVENTS.CARD_BLOCKED, event);
|
||||
this.logger.log(`Emitted CARD_BLOCKED event for card ${updatedCard.id}`);
|
||||
}
|
||||
|
||||
return { id: card.id, status, description };
|
||||
}
|
||||
|
||||
async getEmbossingInformation(customerId: string) {
|
||||
@ -179,13 +216,64 @@ export class CardService {
|
||||
throw new BadRequestException('CARD.INSUFFICIENT_BALANCE');
|
||||
}
|
||||
|
||||
const finalAmount = Decimal(amount).plus(card.limit);
|
||||
await Promise.all([
|
||||
this.neoleapService.updateCardControl(card.cardReference, finalAmount.toNumber()),
|
||||
this.updateCardLimit(card.id, finalAmount.toNumber()),
|
||||
this.accountService.increaseReservedBalance(fundingAccount, amount),
|
||||
this.transactionService.createInternalChildTransaction(card.id, amount),
|
||||
]);
|
||||
// Validate card reference exists
|
||||
if (!card.cardReference) {
|
||||
this.logger.error(`Card ${card.id} does not have a cardReference`);
|
||||
throw new BadRequestException('CARD.INVALID_CARD_REFERENCE');
|
||||
}
|
||||
|
||||
// Validate card limit is a valid number
|
||||
const cardLimit = card.limit || 0;
|
||||
if (isNaN(cardLimit) || cardLimit < 0) {
|
||||
this.logger.error(`Card ${card.id} has invalid limit: ${cardLimit}`);
|
||||
throw new BadRequestException('CARD.INVALID_CARD_LIMIT');
|
||||
}
|
||||
|
||||
const finalAmount = Decimal(amount).plus(cardLimit);
|
||||
const finalAmountNumber = finalAmount.toNumber();
|
||||
|
||||
// Validate final amount is positive
|
||||
if (finalAmountNumber <= 0 || !isFinite(finalAmountNumber)) {
|
||||
this.logger.error(`Invalid final amount calculated: ${finalAmountNumber} (amount: ${amount}, limit: ${cardLimit})`);
|
||||
throw new BadRequestException('CARD.INVALID_AMOUNT');
|
||||
}
|
||||
|
||||
this.logger.debug(`Updating card control - cardReference: ${card.cardReference}, finalAmount: ${finalAmountNumber}`);
|
||||
|
||||
// Check if child and parent share the same account
|
||||
const isSharedAccount = card.parentId && card.account.id === fundingAccount.id;
|
||||
|
||||
this.logger.debug(
|
||||
`Account structure - Child account: ${card.account.id}, Parent account: ${fundingAccount.id}, ` +
|
||||
`Shared: ${isSharedAccount ? 'YES' : 'NO'}`
|
||||
);
|
||||
|
||||
// First, ensure all external operations succeed before creating transaction
|
||||
if (isSharedAccount) {
|
||||
// Shared account: Only update card limit and reserved balance
|
||||
// Money is already in the shared account, just allocate it to the child
|
||||
this.logger.debug(`Shared account detected - only updating card limit and reserved balance`);
|
||||
await Promise.all([
|
||||
this.neoleapService.updateCardControl(card.cardReference, finalAmountNumber),
|
||||
this.updateCardLimit(card.id, finalAmountNumber),
|
||||
this.accountService.increaseReservedBalance(fundingAccount, amount),
|
||||
]);
|
||||
} else {
|
||||
// Separate accounts: Transfer money from parent to child
|
||||
this.logger.debug(`Separate accounts - transferring money from parent to child`);
|
||||
await Promise.all([
|
||||
this.neoleapService.updateCardControl(card.cardReference, finalAmountNumber),
|
||||
this.updateCardLimit(card.id, finalAmountNumber),
|
||||
this.accountService.increaseReservedBalance(fundingAccount, amount),
|
||||
// Increase child account balance
|
||||
this.accountService.creditAccountBalance(card.account.accountReference, amount),
|
||||
// Decrease parent account balance
|
||||
this.accountService.decreaseAccountBalance(fundingAccount.accountReference, amount),
|
||||
]);
|
||||
}
|
||||
|
||||
// Only create transaction and emit event after all operations succeed
|
||||
await this.transactionService.createInternalChildTransaction(card.id, amount);
|
||||
|
||||
return finalAmount.toNumber();
|
||||
}
|
||||
|
||||
@ -110,9 +110,12 @@ export class TransactionService {
|
||||
const card = await this.cardService.getCardById(cardId);
|
||||
const transaction = await this.transactionRepository.createInternalChildTransaction(card, amount);
|
||||
|
||||
// Reload card to get updated account balance after the transfer
|
||||
const cardWithUpdatedBalance = await this.cardService.getCardById(cardId);
|
||||
|
||||
const event: ITransactionCreatedEvent = {
|
||||
transaction,
|
||||
card,
|
||||
card: cardWithUpdatedBalance, // Use card with updated balance
|
||||
isTopUp: true,
|
||||
isChildSpending: true,
|
||||
timestamp: new Date(),
|
||||
|
||||
@ -1,9 +1,12 @@
|
||||
import { Controller, Get, UseGuards } from '@nestjs/common';
|
||||
import { BadRequestException, Controller, Get, Req, UseGuards } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { Request } from 'express';
|
||||
import { AccessTokenGuard } from '~/common/guards';
|
||||
import { ApiDataArrayResponse } from '~/core/decorators';
|
||||
import { ApiDataArrayResponse, ApiLangRequestHeader } from '~/core/decorators';
|
||||
import { ResponseFactory } from '~/core/utils';
|
||||
import { DocumentMetaResponseDto } from '~/document/dtos/response';
|
||||
import { HelpSupportFaqResponseDto } from '../dtos/response';
|
||||
import { LookupLanguage } from '../enums';
|
||||
import { LookupService } from '../services';
|
||||
|
||||
@Controller('lookup')
|
||||
@ -29,4 +32,24 @@ export class LookupController {
|
||||
|
||||
return ResponseFactory.dataArray(avatars.map((avatar) => new DocumentMetaResponseDto(avatar)));
|
||||
}
|
||||
|
||||
@UseGuards(AccessTokenGuard)
|
||||
@Get('help-support-faqs')
|
||||
@ApiDataArrayResponse(HelpSupportFaqResponseDto)
|
||||
@ApiLangRequestHeader()
|
||||
getHelpSupportFaqs(@Req() request: Request) {
|
||||
const header = request.headers['accept-language'];
|
||||
const lang = Array.isArray(header) ? header[0] : header;
|
||||
const normalized = (lang || '').trim().toLowerCase();
|
||||
|
||||
if (normalized && normalized !== LookupLanguage.EN && normalized !== LookupLanguage.AR) {
|
||||
throw new BadRequestException('Accept-Language must be "en" or "ar".');
|
||||
}
|
||||
|
||||
const faqs = this.lookupService.getHelpSupportFaqs(
|
||||
normalized === LookupLanguage.AR ? LookupLanguage.AR : LookupLanguage.EN,
|
||||
);
|
||||
|
||||
return ResponseFactory.dataArray(faqs);
|
||||
}
|
||||
}
|
||||
|
||||
72
src/common/modules/lookup/data/help-support-faqs.json
Normal file
72
src/common/modules/lookup/data/help-support-faqs.json
Normal file
@ -0,0 +1,72 @@
|
||||
[
|
||||
{
|
||||
"id": "transfer_failed_or_delayed",
|
||||
"question_en": "What happens if a transfer fails or is delayed?",
|
||||
"answer_en": "If a transfer fails, the amount will not be deducted from your balance. If a transfer is delayed, it will usually complete within a short time. If the issue continues, please contact customer support.",
|
||||
"question_ar": "ماذا يحدث إذا فشل التحويل أو تأخر؟",
|
||||
"answer_ar": "إذا فشل التحويل، لن يُخصم المبلغ من رصيدك. إذا تأخر التحويل، فعادةً يكتمل خلال وقت قصير. إذا استمرت المشكلة، يرجى التواصل مع دعم العملاء."
|
||||
},
|
||||
{
|
||||
"id": "same_email_or_phone_multiple_accounts",
|
||||
"question_en": "Can I use the same email or phone number for multiple accounts?",
|
||||
"answer_en": "No. Each email address and phone number can be used for only one account.",
|
||||
"question_ar": "هل يمكنني استخدام نفس البريد الإلكتروني أو رقم الهاتف لعدة حسابات؟",
|
||||
"answer_ar": "لا. يمكن استخدام كل بريد إلكتروني ورقم هاتف لحساب واحد فقط."
|
||||
},
|
||||
{
|
||||
"id": "update_profile_details",
|
||||
"question_en": "How do I update my profile details?",
|
||||
"answer_en": "Go to Profile - Edit Profile, update your details, and save the changes.",
|
||||
"question_ar": "كيف يمكنني تحديث بيانات ملفي الشخصي؟",
|
||||
"answer_ar": "انتقل إلى الملف الشخصي - تعديل الملف الشخصي، حدّث بياناتك ثم احفظ التغييرات."
|
||||
},
|
||||
{
|
||||
"id": "change_email_address",
|
||||
"question_en": "How do I change my email address?",
|
||||
"answer_en": "You can update your email or phone number from Profile - Edit Profile. You may be asked to re-login for security reasons.",
|
||||
"question_ar": "كيف أغيّر عنوان بريدي الإلكتروني؟",
|
||||
"answer_ar": "يمكنك تحديث بريدك الإلكتروني أو رقم هاتفك من الملف الشخصي - تعديل الملف الشخصي. قد يُطلب منك تسجيل الدخول مرة أخرى لأسباب أمنية."
|
||||
},
|
||||
{
|
||||
"id": "change_phone_number",
|
||||
"question_en": "How do I change my phone number?",
|
||||
"answer_en": "You cannot update your phone number",
|
||||
"question_ar": "كيف أغيّر رقم هاتفي؟",
|
||||
"answer_ar": "لا يمكنك تحديث رقم هاتفك."
|
||||
},
|
||||
{
|
||||
"id": "activate_card",
|
||||
"question_en": "How do I activate my card?",
|
||||
"answer_en": "Transfer 20 SAR to activate your card. Once completed, your card will become active.",
|
||||
"question_ar": "كيف أقوم بتفعيل بطاقتي؟",
|
||||
"answer_ar": "حوّل 20 ريالًا سعوديًا لتفعيل بطاقتك. بعد إتمام التحويل، ستصبح البطاقة نشطة."
|
||||
},
|
||||
{
|
||||
"id": "why_transfer_20_sar",
|
||||
"question_en": "Why do I need to transfer 20 SAR to activate the card?",
|
||||
"answer_en": "This amount is required to activate the card and enable usage.",
|
||||
"question_ar": "لماذا يجب علي تحويل 20 ريالًا سعوديًا لتفعيل البطاقة؟",
|
||||
"answer_ar": "هذا المبلغ مطلوب لتفعيل البطاقة وتمكين استخدامها."
|
||||
},
|
||||
{
|
||||
"id": "where_activation_amount_go",
|
||||
"question_en": "Where does the activation amount go?",
|
||||
"answer_en": "The activation amount is collected by the company as part of the card activation process.",
|
||||
"question_ar": "أين يذهب مبلغ التفعيل؟",
|
||||
"answer_ar": "يتم تحصيل مبلغ التفعيل من قبل الشركة كجزء من عملية تفعيل البطاقة."
|
||||
},
|
||||
{
|
||||
"id": "top_up_external_bank",
|
||||
"question_en": "Can I top up from an external bank account?",
|
||||
"answer_en": "Yes. You can transfer funds from your external bank to your ZOD account",
|
||||
"question_ar": "هل يمكنني الشحن من حساب بنكي خارجي؟",
|
||||
"answer_ar": "نعم. يمكنك تحويل الأموال من بنكك الخارجي إلى حسابك في زد."
|
||||
},
|
||||
{
|
||||
"id": "data_security",
|
||||
"question_en": "Is my data secure in the ZOD app?",
|
||||
"answer_en": "Yes. We use secure systems and encryption to protect your data.",
|
||||
"question_ar": "هل بياناتي آمنة في تطبيق زد؟",
|
||||
"answer_ar": "نعم. نستخدم أنظمة آمنة وتشفيرًا لحماية بياناتك."
|
||||
}
|
||||
]
|
||||
@ -0,0 +1,18 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class HelpSupportFaqResponseDto {
|
||||
@ApiProperty()
|
||||
id!: string;
|
||||
|
||||
@ApiProperty()
|
||||
question!: string;
|
||||
|
||||
@ApiProperty()
|
||||
answer!: string;
|
||||
|
||||
constructor(item: { id: string; question: string; answer: string }) {
|
||||
this.id = item.id;
|
||||
this.question = item.question;
|
||||
this.answer = item.answer;
|
||||
}
|
||||
}
|
||||
1
src/common/modules/lookup/dtos/response/index.ts
Normal file
1
src/common/modules/lookup/dtos/response/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './help-support-faq.response.dto';
|
||||
1
src/common/modules/lookup/enums/index.ts
Normal file
1
src/common/modules/lookup/enums/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './lookup-language.enum';
|
||||
4
src/common/modules/lookup/enums/lookup-language.enum.ts
Normal file
4
src/common/modules/lookup/enums/lookup-language.enum.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export enum LookupLanguage {
|
||||
EN = 'en',
|
||||
AR = 'ar',
|
||||
}
|
||||
@ -1,6 +1,9 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { DocumentType } from '~/document/enums';
|
||||
import { DocumentService, OciService } from '~/document/services';
|
||||
import { HelpSupportFaqResponseDto } from '../dtos/response';
|
||||
import { LookupLanguage } from '../enums';
|
||||
import helpSupportFaqs from '../data/help-support-faqs.json';
|
||||
|
||||
@Injectable()
|
||||
export class LookupService {
|
||||
@ -33,4 +36,16 @@ export class LookupService {
|
||||
this.logger.log(`Default tasks logos returned successfully`);
|
||||
return documents;
|
||||
}
|
||||
|
||||
getHelpSupportFaqs(lang: LookupLanguage = LookupLanguage.EN): HelpSupportFaqResponseDto[] {
|
||||
const useArabic = lang === LookupLanguage.AR;
|
||||
|
||||
return helpSupportFaqs.map((faq) =>
|
||||
new HelpSupportFaqResponseDto({
|
||||
id: faq.id,
|
||||
question: useArabic ? faq.question_ar : faq.question_en,
|
||||
answer: useArabic ? faq.answer_ar : faq.answer_en,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -427,10 +427,18 @@ export class NeoLeapService {
|
||||
});
|
||||
} catch (error: any) {
|
||||
if (error.status === 400) {
|
||||
console.error('Error sending request to NeoLeap:', error);
|
||||
throw new BadRequestException(error.response?.data?.ResponseHeader?.ResponseDescription || error.message);
|
||||
const errorMessage = error.response?.data?.ResponseHeader?.ResponseDescription ||
|
||||
error.response?.data?.message ||
|
||||
error.message;
|
||||
const errorCode = error.response?.data?.ResponseHeader?.ResponseCode || 'UNKNOWN';
|
||||
this.logger.error(
|
||||
`NeoLeap API returned 400 error for endpoint ${endpoint}. ` +
|
||||
`Error Code: ${errorCode}, Message: ${errorMessage}. ` +
|
||||
`Payload: ${JSON.stringify(payload)}`
|
||||
);
|
||||
throw new BadRequestException(errorMessage || 'Request failed with status code 400');
|
||||
}
|
||||
console.error('Error sending request to NeoLeap:', error);
|
||||
this.logger.error(`Error sending request to NeoLeap endpoint ${endpoint}:`, error);
|
||||
throw new InternalServerErrorException('Error communicating with NeoLeap service');
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,6 +10,23 @@ export const NOTIFICATION_EVENTS = {
|
||||
MONEY_REQUEST_CREATED: 'notification.money-request.created',
|
||||
MONEY_REQUEST_APPROVED: 'notification.money-request.approved',
|
||||
MONEY_REQUEST_DECLINED: 'notification.money-request.declined',
|
||||
|
||||
// KYC Update events
|
||||
KYC_APPROVED: 'notification.kyc.approved',
|
||||
KYC_REJECTED: 'notification.kyc.rejected',
|
||||
|
||||
// Card Status events
|
||||
CARD_CREATED: 'notification.card.created',
|
||||
CARD_BLOCKED: 'notification.card.blocked',
|
||||
CARD_REISSUED: 'notification.card.reissued',
|
||||
|
||||
// Profile Update events
|
||||
PROFILE_UPDATED: 'notification.profile.updated',
|
||||
|
||||
// System Alert events
|
||||
MAINTENANCE_ALERT: 'notification.system.maintenance',
|
||||
TRANSACTION_FAILED: 'notification.system.transaction-failed',
|
||||
SUSPICIOUS_LOGIN: 'notification.system.suspicious-login',
|
||||
} as const;
|
||||
|
||||
export type NotificationEventName =
|
||||
|
||||
@ -23,6 +23,17 @@ export class NotificationsResponseDto {
|
||||
this.title = notification.title;
|
||||
this.body = notification.message;
|
||||
this.status = notification.status!;
|
||||
this.createdAt = notification.createdAt;
|
||||
|
||||
// Use event timestamp from data if available, otherwise use notification creation time
|
||||
// This ensures notifications show when the event occurred, not when notification was saved
|
||||
// Note: Timestamps are stored in UTC. The client should convert to the user's local timezone.
|
||||
if (notification.data?.timestamp) {
|
||||
// Parse the ISO string timestamp (which is in UTC)
|
||||
// The client should convert this to the user's local timezone based on their device settings
|
||||
this.createdAt = new Date(notification.data.timestamp);
|
||||
} else {
|
||||
// Use notification creation time (also in UTC)
|
||||
this.createdAt = notification.createdAt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,10 +6,14 @@ export enum NotificationScope {
|
||||
OTP = 'OTP',
|
||||
USER_INVITED = 'USER_INVITED',
|
||||
|
||||
// Transaction notifications - Top-up
|
||||
// Transaction notifications - Top-up (external funds)
|
||||
CHILD_TOP_UP = 'CHILD_TOP_UP',
|
||||
PARENT_TOP_UP_CONFIRMATION = 'PARENT_TOP_UP_CONFIRMATION',
|
||||
|
||||
// Transaction notifications - Internal Transfer (parent to child)
|
||||
CHILD_INTERNAL_TRANSFER = 'CHILD_INTERNAL_TRANSFER',
|
||||
PARENT_INTERNAL_TRANSFER = 'PARENT_INTERNAL_TRANSFER',
|
||||
|
||||
// Transaction notifications - Spending
|
||||
CHILD_SPENDING = 'CHILD_SPENDING',
|
||||
PARENT_SPENDING_ALERT = 'PARENT_SPENDING_ALERT',
|
||||
@ -18,6 +22,23 @@ export enum NotificationScope {
|
||||
MONEY_REQUEST_CREATED = 'MONEY_REQUEST_CREATED',
|
||||
MONEY_REQUEST_APPROVED = 'MONEY_REQUEST_APPROVED',
|
||||
MONEY_REQUEST_DECLINED = 'MONEY_REQUEST_DECLINED',
|
||||
|
||||
// KYC Update notifications
|
||||
KYC_APPROVED = 'KYC_APPROVED',
|
||||
KYC_REJECTED = 'KYC_REJECTED',
|
||||
|
||||
// Card Status notifications
|
||||
CARD_CREATED = 'CARD_CREATED',
|
||||
CARD_BLOCKED = 'CARD_BLOCKED',
|
||||
CARD_REISSUED = 'CARD_REISSUED',
|
||||
|
||||
// Profile Update notifications
|
||||
PROFILE_UPDATED = 'PROFILE_UPDATED',
|
||||
|
||||
// System Alert notifications
|
||||
MAINTENANCE_ALERT = 'MAINTENANCE_ALERT',
|
||||
TRANSACTION_FAILED = 'TRANSACTION_FAILED',
|
||||
SUSPICIOUS_LOGIN = 'SUSPICIOUS_LOGIN',
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
import { Transaction } from '~/card/entities/transaction.entity';
|
||||
import { Card } from '~/card/entities/card.entity';
|
||||
import { MoneyRequest } from '~/money-request/entities/money-request.entity';
|
||||
import { Customer } from '~/customer/entities';
|
||||
import { KycStatus } from '~/customer/enums';
|
||||
|
||||
/**
|
||||
* Event payload for when a transaction is created
|
||||
@ -61,3 +63,159 @@ export interface IMoneyRequestDeclinedEvent {
|
||||
/** When the event occurred */
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event payload for when KYC is approved
|
||||
* Used to notify users when their KYC verification is approved
|
||||
*/
|
||||
export interface IKycApprovedEvent {
|
||||
/** The customer whose KYC was approved */
|
||||
customer: Customer;
|
||||
|
||||
/** Previous KYC status */
|
||||
previousStatus: KycStatus;
|
||||
|
||||
/** When the event occurred */
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event payload for when KYC is rejected
|
||||
* Used to notify users when their KYC verification is rejected
|
||||
*/
|
||||
export interface IKycRejectedEvent {
|
||||
/** The customer whose KYC was rejected */
|
||||
customer: Customer;
|
||||
|
||||
/** Previous KYC status */
|
||||
previousStatus: KycStatus;
|
||||
|
||||
/** Rejection reason (if provided) */
|
||||
rejectionReason?: string;
|
||||
|
||||
/** When the event occurred */
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event payload for when a card is created
|
||||
* Used to notify users when their card is successfully created
|
||||
*/
|
||||
export interface ICardCreatedEvent {
|
||||
/** The card that was created */
|
||||
card: Card;
|
||||
|
||||
/** When the event occurred */
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event payload for when a card is blocked
|
||||
* Used to notify users when their card is blocked
|
||||
*/
|
||||
export interface ICardBlockedEvent {
|
||||
/** The card that was blocked */
|
||||
card: Card;
|
||||
|
||||
/** Previous card status */
|
||||
previousStatus: string;
|
||||
|
||||
/** Block reason/description */
|
||||
blockReason?: string;
|
||||
|
||||
/** When the event occurred */
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event payload for when a card is reissued
|
||||
* Used to notify users when their card is reissued
|
||||
*/
|
||||
export interface ICardReissuedEvent {
|
||||
/** The new card that was issued */
|
||||
card: Card;
|
||||
|
||||
/** The old card that was replaced */
|
||||
oldCardId?: string;
|
||||
|
||||
/** When the event occurred */
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event payload for when a user profile is updated
|
||||
* Used to notify users when their profile information is changed
|
||||
*/
|
||||
export interface IProfileUpdatedEvent {
|
||||
/** The user whose profile was updated */
|
||||
user: any;
|
||||
|
||||
/** Fields that were updated */
|
||||
updatedFields: string[];
|
||||
|
||||
/** When the event occurred */
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event payload for system maintenance alerts
|
||||
* Used to notify users about scheduled or unscheduled maintenance
|
||||
*/
|
||||
export interface IMaintenanceAlertEvent {
|
||||
/** User ID to notify (null for broadcast to all users) */
|
||||
userId: string | null;
|
||||
|
||||
/** Maintenance message */
|
||||
message: string;
|
||||
|
||||
/** Scheduled start time */
|
||||
startTime?: Date;
|
||||
|
||||
/** Scheduled end time */
|
||||
endTime?: Date;
|
||||
|
||||
/** When the event occurred */
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event payload for failed transaction alerts
|
||||
* Used to notify users when a transaction fails
|
||||
*/
|
||||
export interface ITransactionFailedEvent {
|
||||
/** The user whose transaction failed */
|
||||
userId: string;
|
||||
|
||||
/** Transaction details */
|
||||
transactionId?: string;
|
||||
|
||||
/** Failure reason */
|
||||
reason: string;
|
||||
|
||||
/** Transaction amount (if applicable) */
|
||||
amount?: number;
|
||||
|
||||
/** When the event occurred */
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event payload for suspicious login detection
|
||||
* Used to notify users about suspicious login attempts
|
||||
*/
|
||||
export interface ISuspiciousLoginEvent {
|
||||
/** The user whose account had suspicious activity */
|
||||
userId: string;
|
||||
|
||||
/** IP address of the login attempt */
|
||||
ipAddress?: string;
|
||||
|
||||
/** Location of the login attempt */
|
||||
location?: string;
|
||||
|
||||
/** Device information */
|
||||
device?: string;
|
||||
|
||||
/** When the event occurred */
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
@ -0,0 +1,162 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { I18nService } from 'nestjs-i18n';
|
||||
import { NotificationFactory, NotificationPreferences } from '../services/notification-factory.service';
|
||||
import { UserService } from '~/user/services/user.service';
|
||||
import { NOTIFICATION_EVENTS } from '../constants/event-names.constant';
|
||||
import { ICardBlockedEvent, ICardCreatedEvent } from '../interfaces/notification-events.interface';
|
||||
import { NotificationScope } from '../enums/notification-scope.enum';
|
||||
import { User } from '~/user/entities';
|
||||
import { UserLocale } from '~/core/enums/user-locale.enum';
|
||||
|
||||
@Injectable()
|
||||
export class CardNotificationListener {
|
||||
private readonly logger = new Logger(CardNotificationListener.name);
|
||||
|
||||
constructor(
|
||||
private readonly notificationFactory: NotificationFactory,
|
||||
private readonly userService: UserService,
|
||||
private readonly i18n: I18nService,
|
||||
) {}
|
||||
|
||||
@OnEvent(NOTIFICATION_EVENTS.CARD_CREATED)
|
||||
async handleCardCreated(event: ICardCreatedEvent): Promise<void> {
|
||||
try {
|
||||
const { card } = event;
|
||||
const user = card?.customer?.user;
|
||||
|
||||
if (!user) {
|
||||
this.logger.warn(`No user found for card ${card.id}, skipping card created notification`);
|
||||
return;
|
||||
}
|
||||
|
||||
const locale = this.getUserLocale(user);
|
||||
const lastFourDigits = card.lastFourDigits;
|
||||
|
||||
let title: string;
|
||||
let message: string;
|
||||
|
||||
try {
|
||||
title = this.i18n.t('app.NOTIFICATION.CARD_CREATED_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.CARD_CREATED_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
lastFourDigits: lastFourDigits,
|
||||
},
|
||||
});
|
||||
} catch (i18nError: any) {
|
||||
this.logger.error(
|
||||
`[CardNotificationListener] i18n error for user ${user.id}: ${i18nError?.message || 'Unknown i18n error'}. Falling back to English.`,
|
||||
i18nError?.stack
|
||||
);
|
||||
title = 'Card Created';
|
||||
message = `Your card ending in ${lastFourDigits} has been created successfully. You can start using it once it's activated.`;
|
||||
}
|
||||
|
||||
this.logger.debug(
|
||||
`Notifying user (user ${user.id}): Card created - ${lastFourDigits}`
|
||||
);
|
||||
|
||||
await this.notificationFactory.send({
|
||||
userId: user.id,
|
||||
title,
|
||||
message,
|
||||
scope: NotificationScope.CARD_CREATED,
|
||||
preferences: this.getUserPreferences(user),
|
||||
data: {
|
||||
cardId: card.id,
|
||||
lastFourDigits: lastFourDigits,
|
||||
cardReference: card.cardReference,
|
||||
status: card.status,
|
||||
timestamp: event.timestamp.toISOString(),
|
||||
action: 'VIEW_CARD',
|
||||
},
|
||||
});
|
||||
|
||||
this.logger.log(`✅ Notified user ${user.id} about card creation`);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to process card created notification: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@OnEvent(NOTIFICATION_EVENTS.CARD_BLOCKED)
|
||||
async handleCardBlocked(event: ICardBlockedEvent): Promise<void> {
|
||||
try {
|
||||
const { card, blockReason } = event;
|
||||
const user = card?.customer?.user;
|
||||
|
||||
if (!user) {
|
||||
this.logger.warn(`No user found for card ${card.id}, skipping card blocked notification`);
|
||||
return;
|
||||
}
|
||||
|
||||
const locale = this.getUserLocale(user);
|
||||
const lastFourDigits = card.lastFourDigits;
|
||||
const reason = blockReason || 'Card has been blocked';
|
||||
|
||||
let title: string;
|
||||
let message: string;
|
||||
|
||||
try {
|
||||
title = this.i18n.t('app.NOTIFICATION.CARD_BLOCKED_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.CARD_BLOCKED_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
lastFourDigits: lastFourDigits,
|
||||
reason: reason,
|
||||
},
|
||||
});
|
||||
} catch (i18nError: any) {
|
||||
this.logger.error(
|
||||
`[CardNotificationListener] i18n error for user ${user.id}: ${i18nError?.message || 'Unknown i18n error'}. Falling back to English.`,
|
||||
i18nError?.stack
|
||||
);
|
||||
title = 'Card Blocked';
|
||||
message = `Your card ending in ${lastFourDigits} has been blocked. Reason: ${reason}. Please contact support for assistance.`;
|
||||
}
|
||||
|
||||
this.logger.debug(
|
||||
`Notifying user (user ${user.id}): Card blocked - ${lastFourDigits}, reason: ${reason}`
|
||||
);
|
||||
|
||||
await this.notificationFactory.send({
|
||||
userId: user.id,
|
||||
title,
|
||||
message,
|
||||
scope: NotificationScope.CARD_BLOCKED,
|
||||
preferences: this.getUserPreferences(user),
|
||||
data: {
|
||||
cardId: card.id,
|
||||
lastFourDigits: lastFourDigits,
|
||||
cardReference: card.cardReference,
|
||||
status: card.status,
|
||||
blockReason: reason,
|
||||
timestamp: event.timestamp.toISOString(),
|
||||
action: 'CONTACT_SUPPORT',
|
||||
},
|
||||
});
|
||||
|
||||
this.logger.log(`✅ Notified user ${user.id} about card being blocked`);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to process card blocked notification: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private getUserPreferences(user: User): NotificationPreferences {
|
||||
return {
|
||||
isPushEnabled: user.isPushEnabled,
|
||||
isEmailEnabled: user.isEmailEnabled,
|
||||
isSmsEnabled: user.isSmsEnabled,
|
||||
};
|
||||
}
|
||||
|
||||
private getUserLocale(user: User): UserLocale {
|
||||
return UserLocale.ENGLISH;
|
||||
}
|
||||
}
|
||||
@ -1,3 +1,7 @@
|
||||
export * from './notification-created.listener';
|
||||
export * from './transaction-notification.listener';
|
||||
export * from './money-request-notification.listener';
|
||||
export * from './kyc-notification.listener';
|
||||
export * from './card-notification.listener';
|
||||
export * from './profile-notification.listener';
|
||||
export * from './system-alert-notification.listener';
|
||||
@ -0,0 +1,233 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { I18nService } from 'nestjs-i18n';
|
||||
import { NotificationFactory, NotificationPreferences } from '../services/notification-factory.service';
|
||||
import { UserService } from '~/user/services/user.service';
|
||||
import { NOTIFICATION_EVENTS } from '../constants/event-names.constant';
|
||||
import {
|
||||
IKycApprovedEvent,
|
||||
IKycRejectedEvent,
|
||||
} from '../interfaces/notification-events.interface';
|
||||
import { NotificationScope } from '../enums/notification-scope.enum';
|
||||
import { User } from '~/user/entities';
|
||||
import { UserLocale } from '~/core/enums/user-locale.enum';
|
||||
|
||||
/**
|
||||
* KycNotificationListener
|
||||
*
|
||||
* Handles notifications for KYC update events.
|
||||
* Notifies users when their KYC verification is approved or rejected.
|
||||
*
|
||||
* Responsibilities:
|
||||
* - Listen for KYC approval/rejection events
|
||||
* - Determine notification recipient (the user whose KYC was updated)
|
||||
* - Construct appropriate messages with rejection reason if applicable
|
||||
* - Fetch user preferences
|
||||
* - Call NotificationFactory to send
|
||||
*/
|
||||
@Injectable()
|
||||
export class KycNotificationListener {
|
||||
private readonly logger = new Logger(KycNotificationListener.name);
|
||||
|
||||
constructor(
|
||||
private readonly notificationFactory: NotificationFactory,
|
||||
private readonly userService: UserService,
|
||||
private readonly i18n: I18nService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Handle KYC approved event
|
||||
* Notifies user when their KYC verification is approved
|
||||
*/
|
||||
@OnEvent(NOTIFICATION_EVENTS.KYC_APPROVED)
|
||||
async handleKycApproved(event: IKycApprovedEvent): Promise<void> {
|
||||
try {
|
||||
const { customer } = event;
|
||||
|
||||
this.logger.log(
|
||||
`Processing KYC approved notification for customer ${customer.id}`
|
||||
);
|
||||
|
||||
await this.notifyUserOfKycApproval(customer);
|
||||
|
||||
this.logger.log(
|
||||
`KYC approved notification processed successfully for customer ${customer.id}`
|
||||
);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to process KYC approved notification: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle KYC rejected event
|
||||
* Notifies user when their KYC verification is rejected
|
||||
*/
|
||||
@OnEvent(NOTIFICATION_EVENTS.KYC_REJECTED)
|
||||
async handleKycRejected(event: IKycRejectedEvent): Promise<void> {
|
||||
try {
|
||||
const { customer, rejectionReason } = event;
|
||||
|
||||
this.logger.log(
|
||||
`Processing KYC rejected notification for customer ${customer.id} - Reason: ${rejectionReason || 'Not provided'}`
|
||||
);
|
||||
|
||||
await this.notifyUserOfKycRejection(customer, rejectionReason);
|
||||
|
||||
this.logger.log(
|
||||
`KYC rejected notification processed successfully for customer ${customer.id}`
|
||||
);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to process KYC rejected notification: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify user when their KYC is approved
|
||||
*/
|
||||
private async notifyUserOfKycApproval(customer: any): Promise<void> {
|
||||
try {
|
||||
const user = customer?.user;
|
||||
if (!user) {
|
||||
this.logger.warn(`No user found for customer ${customer.id}, skipping notification`);
|
||||
return;
|
||||
}
|
||||
|
||||
const locale = this.getUserLocale(user);
|
||||
|
||||
let title: string;
|
||||
let message: string;
|
||||
|
||||
try {
|
||||
title = this.i18n.t('app.NOTIFICATION.KYC_APPROVED_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.KYC_APPROVED_MESSAGE', {
|
||||
lang: locale,
|
||||
});
|
||||
} catch (i18nError: any) {
|
||||
this.logger.error(
|
||||
`[KycNotificationListener] i18n error for user ${user.id}: ${i18nError?.message || 'Unknown i18n error'}. Falling back to English.`,
|
||||
i18nError?.stack
|
||||
);
|
||||
title = 'KYC Verification Approved';
|
||||
message = 'Your KYC verification has been approved. You can now use all features of the app.';
|
||||
}
|
||||
|
||||
this.logger.debug(
|
||||
`Notifying user (user ${user.id}): KYC approved`
|
||||
);
|
||||
|
||||
await this.notificationFactory.send({
|
||||
userId: user.id,
|
||||
title,
|
||||
message,
|
||||
scope: NotificationScope.KYC_APPROVED,
|
||||
preferences: this.getUserPreferences(user),
|
||||
data: {
|
||||
customerId: customer.id,
|
||||
kycStatus: 'APPROVED',
|
||||
timestamp: new Date().toISOString(),
|
||||
type: 'KYC_APPROVED',
|
||||
action: 'VIEW_PROFILE',
|
||||
},
|
||||
});
|
||||
|
||||
this.logger.log(`✅ Notified user ${user.id} about KYC approval`);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to notify user of KYC approval: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify user when their KYC is rejected
|
||||
*/
|
||||
private async notifyUserOfKycRejection(customer: any, rejectionReason?: string): Promise<void> {
|
||||
try {
|
||||
const user = customer?.user;
|
||||
if (!user) {
|
||||
this.logger.warn(`No user found for customer ${customer.id}, skipping notification`);
|
||||
return;
|
||||
}
|
||||
|
||||
const locale = this.getUserLocale(user);
|
||||
const reason = rejectionReason || customer.rejectionReason || 'KYC verification failed';
|
||||
|
||||
let title: string;
|
||||
let message: string;
|
||||
|
||||
try {
|
||||
title = this.i18n.t('app.NOTIFICATION.KYC_REJECTED_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.KYC_REJECTED_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
reason: reason,
|
||||
},
|
||||
});
|
||||
} catch (i18nError: any) {
|
||||
this.logger.error(
|
||||
`[KycNotificationListener] i18n error for user ${user.id}: ${i18nError?.message || 'Unknown i18n error'}. Falling back to English.`,
|
||||
i18nError?.stack
|
||||
);
|
||||
title = 'KYC Verification Rejected';
|
||||
message = `Your KYC verification has been rejected. Reason: ${reason}. Please review your information and try again.`;
|
||||
}
|
||||
|
||||
this.logger.debug(
|
||||
`Notifying user (user ${user.id}): KYC rejected - ${reason}`
|
||||
);
|
||||
|
||||
await this.notificationFactory.send({
|
||||
userId: user.id,
|
||||
title,
|
||||
message,
|
||||
scope: NotificationScope.KYC_REJECTED,
|
||||
preferences: this.getUserPreferences(user),
|
||||
data: {
|
||||
customerId: customer.id,
|
||||
kycStatus: 'REJECTED',
|
||||
rejectionReason: reason,
|
||||
timestamp: new Date().toISOString(),
|
||||
type: 'KYC_REJECTED',
|
||||
action: 'RETRY_KYC',
|
||||
},
|
||||
});
|
||||
|
||||
this.logger.log(`✅ Notified user ${user.id} about KYC rejection`);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to notify user of KYC rejection: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract user preferences from User entity
|
||||
* Converts User properties to NotificationPreferences interface
|
||||
*/
|
||||
private getUserPreferences(user: User): NotificationPreferences {
|
||||
return {
|
||||
isPushEnabled: user.isPushEnabled,
|
||||
isEmailEnabled: user.isEmailEnabled,
|
||||
isSmsEnabled: user.isSmsEnabled,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user locale for i18n translations
|
||||
* Defaults to English if not specified
|
||||
* TODO: Add locale field to User entity in the future
|
||||
*/
|
||||
private getUserLocale(user: User): UserLocale {
|
||||
// For now, default to English
|
||||
// In the future, this can read from user.locale or user.preferences.locale
|
||||
return UserLocale.ENGLISH;
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { I18nService } from 'nestjs-i18n';
|
||||
import { NotificationFactory, NotificationPreferences } from '../services/notification-factory.service';
|
||||
import { UserService } from '~/user/services/user.service';
|
||||
import { NOTIFICATION_EVENTS } from '../constants/event-names.constant';
|
||||
@ -10,6 +11,9 @@ import {
|
||||
} from '../interfaces/notification-events.interface';
|
||||
import { NotificationScope } from '../enums/notification-scope.enum';
|
||||
import { User } from '~/user/entities';
|
||||
import { MoneyRequest } from '~/money-request/entities/money-request.entity';
|
||||
import { UserLocale } from '~/core/enums/user-locale.enum';
|
||||
import { formatCurrencyAmount, getCurrency } from '~/common/utils/currency.util';
|
||||
|
||||
/**
|
||||
* MoneyRequestNotificationListener
|
||||
@ -31,6 +35,7 @@ export class MoneyRequestNotificationListener {
|
||||
constructor(
|
||||
private readonly notificationFactory: NotificationFactory,
|
||||
private readonly userService: UserService,
|
||||
private readonly i18n: I18nService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@ -130,22 +135,51 @@ export class MoneyRequestNotificationListener {
|
||||
const childName = childUser?.firstName || 'Your child';
|
||||
const amount = typeof moneyRequest.amount === 'string' ? parseFloat(moneyRequest.amount) : moneyRequest.amount;
|
||||
const reason = moneyRequest.reason || 'No reason provided';
|
||||
const accountCurrency = child?.customer?.cards?.[0]?.account?.currency;
|
||||
const currency = getCurrency(accountCurrency, null, 'SAR');
|
||||
const formattedAmount = formatCurrencyAmount(amount, currency);
|
||||
|
||||
const locale = this.getUserLocale(parentUser);
|
||||
|
||||
this.logger.debug(
|
||||
`Notifying parent (user ${parentUser.id}): ${childName} requested $${amount} - ${reason}`
|
||||
`Notifying parent (user ${parentUser.id}): ${childName} requested ${formattedAmount} ${currency} for ${reason}`
|
||||
);
|
||||
|
||||
let title: string;
|
||||
let message: string;
|
||||
|
||||
try {
|
||||
title = this.i18n.t('app.NOTIFICATION.MONEY_REQUEST_CREATED_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.MONEY_REQUEST_CREATED_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
childName: childName,
|
||||
amount: formattedAmount,
|
||||
currency: currency,
|
||||
reason: reason,
|
||||
},
|
||||
});
|
||||
} catch (i18nError: any) {
|
||||
this.logger.error(
|
||||
`[MoneyRequestNotificationListener] i18n error for parent ${parentUser.id}: ${i18nError?.message || 'Unknown i18n error'}. Falling back to English.`,
|
||||
i18nError?.stack
|
||||
);
|
||||
title = 'Money Request';
|
||||
message = `${childName} has requested ${formattedAmount} ${currency} for ${reason}.`;
|
||||
}
|
||||
|
||||
await this.notificationFactory.send({
|
||||
userId: parentUser.id,
|
||||
title: 'Money Request',
|
||||
message: `${childName} requested $${amount.toFixed(2)}. Reason: ${reason}`,
|
||||
title,
|
||||
message,
|
||||
scope: NotificationScope.MONEY_REQUEST_CREATED,
|
||||
preferences: this.getUserPreferences(parentUser),
|
||||
data: {
|
||||
moneyRequestId: moneyRequest.id,
|
||||
childId: childUser?.id,
|
||||
childName: childName,
|
||||
amount: amount.toString(),
|
||||
amount: formattedAmount,
|
||||
currency: currency,
|
||||
reason: reason,
|
||||
timestamp: moneyRequest.createdAt.toISOString(),
|
||||
type: 'MONEY_REQUEST',
|
||||
@ -176,20 +210,24 @@ export class MoneyRequestNotificationListener {
|
||||
}
|
||||
|
||||
const amount = typeof moneyRequest.amount === 'string' ? parseFloat(moneyRequest.amount) : moneyRequest.amount;
|
||||
const accountCurrency = child?.customer?.cards?.[0]?.account?.currency;
|
||||
const currency = getCurrency(accountCurrency, null, 'SAR');
|
||||
const formattedAmount = formatCurrencyAmount(amount, currency);
|
||||
|
||||
this.logger.debug(
|
||||
`Notifying child (user ${childUser.id}): Money request of $${amount} was approved`
|
||||
`Notifying child (user ${childUser.id}): Money request of ${formattedAmount} ${currency} was approved`
|
||||
);
|
||||
|
||||
await this.notificationFactory.send({
|
||||
userId: childUser.id,
|
||||
title: 'Money Request Approved',
|
||||
message: `Your request for $${amount.toFixed(2)} has been approved. The money has been added to your account.`,
|
||||
message: `Your request for ${formattedAmount} ${currency} has been approved. The money has been added to your account.`,
|
||||
scope: NotificationScope.MONEY_REQUEST_APPROVED,
|
||||
preferences: this.getUserPreferences(childUser),
|
||||
data: {
|
||||
moneyRequestId: moneyRequest.id,
|
||||
amount: amount.toString(),
|
||||
amount: formattedAmount,
|
||||
currency: currency,
|
||||
timestamp: moneyRequest.updatedAt.toISOString(),
|
||||
type: 'MONEY_REQUEST_APPROVED',
|
||||
action: 'VIEW_MONEY_REQUEST',
|
||||
@ -219,21 +257,48 @@ export class MoneyRequestNotificationListener {
|
||||
}
|
||||
|
||||
const amount = typeof moneyRequest.amount === 'string' ? parseFloat(moneyRequest.amount) : moneyRequest.amount;
|
||||
const accountCurrency = child?.customer?.cards?.[0]?.account?.currency;
|
||||
const currency = getCurrency(accountCurrency, null, 'SAR');
|
||||
const formattedAmount = formatCurrencyAmount(amount, currency);
|
||||
const reason = rejectionReason || 'No reason provided';
|
||||
const locale = this.getUserLocale(childUser);
|
||||
|
||||
this.logger.debug(
|
||||
`Notifying child (user ${childUser.id}): Money request of $${amount} was declined`
|
||||
`Notifying child (user ${childUser.id}): Money request of ${formattedAmount} ${currency} was declined`
|
||||
);
|
||||
|
||||
let title: string;
|
||||
let message: string;
|
||||
|
||||
try {
|
||||
title = this.i18n.t('app.NOTIFICATION.MONEY_REQUEST_DECLINED_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.MONEY_REQUEST_DECLINED_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
amount: formattedAmount,
|
||||
currency: currency,
|
||||
reason: reason,
|
||||
},
|
||||
});
|
||||
} catch (i18nError: any) {
|
||||
this.logger.error(
|
||||
`[MoneyRequestNotificationListener] i18n error for child ${childUser.id}: ${i18nError?.message || 'Unknown i18n error'}. Falling back to English.`,
|
||||
i18nError?.stack
|
||||
);
|
||||
title = 'Money Request Declined';
|
||||
message = `Your request for ${formattedAmount} ${currency} has been declined. Reason: ${reason}`;
|
||||
}
|
||||
|
||||
await this.notificationFactory.send({
|
||||
userId: childUser.id,
|
||||
title: 'Money Request Declined',
|
||||
message: `Your request for $${amount.toFixed(2)} has been declined. Reason: ${reason}`,
|
||||
title,
|
||||
message,
|
||||
scope: NotificationScope.MONEY_REQUEST_DECLINED,
|
||||
preferences: this.getUserPreferences(childUser),
|
||||
data: {
|
||||
moneyRequestId: moneyRequest.id,
|
||||
amount: amount.toString(),
|
||||
amount: formattedAmount,
|
||||
currency: currency,
|
||||
rejectionReason: reason,
|
||||
timestamp: moneyRequest.updatedAt.toISOString(),
|
||||
type: 'MONEY_REQUEST_DECLINED',
|
||||
@ -261,4 +326,10 @@ export class MoneyRequestNotificationListener {
|
||||
isSmsEnabled: user.isSmsEnabled,
|
||||
};
|
||||
}
|
||||
|
||||
private getUserLocale(user: User): UserLocale {
|
||||
// For now, default to English
|
||||
// In the future, this can read from user.locale or user.preferences.locale
|
||||
return UserLocale.ENGLISH;
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import { EventType, NotificationChannel, NotificationScope } from '~/common/modu
|
||||
import { FirebaseService, TwilioService } from '~/common/modules/notification/services';
|
||||
import { IEventInterface } from '~/common/redis/interface';
|
||||
import { DeviceService } from '~/user/services';
|
||||
import { UserService } from '~/user/services/user.service';
|
||||
|
||||
@Injectable()
|
||||
export class NotificationCreatedListener {
|
||||
@ -16,6 +17,7 @@ export class NotificationCreatedListener {
|
||||
private readonly deviceService: DeviceService,
|
||||
private readonly mailerService: MailerService,
|
||||
private readonly firebaseService: FirebaseService,
|
||||
private readonly userService: UserService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@ -60,26 +62,48 @@ export class NotificationCreatedListener {
|
||||
body: string,
|
||||
data?: Record<string, any>,
|
||||
) {
|
||||
this.logger.log(`Sending push notification to user ${userId}`);
|
||||
const tokens = await this.deviceService.getTokens(userId);
|
||||
try {
|
||||
// Check if user has push notifications enabled
|
||||
const user = await this.userService.findUser({ id: userId });
|
||||
if (!user) {
|
||||
this.logger.warn(`User ${userId} not found, skipping push notification`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!tokens.length) {
|
||||
this.logger.log(`No device tokens found for user ${userId}, but notification was created in the DB.`);
|
||||
return;
|
||||
if (!user.isPushEnabled) {
|
||||
this.logger.log(
|
||||
`Push notifications disabled for user ${userId}, notification saved to DB but push not sent`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
this.logger.log(`Sending push notification to user ${userId}`);
|
||||
const tokens = await this.deviceService.getTokens(userId);
|
||||
|
||||
if (!tokens.length) {
|
||||
this.logger.log(`No device tokens found for user ${userId}, but notification was created in the DB.`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert data to string values (Firebase requires string values in data payload)
|
||||
const stringData: Record<string, string> | undefined = data
|
||||
? Object.entries(data).reduce(
|
||||
(acc, [key, value]) => {
|
||||
acc[key] = String(value);
|
||||
return acc;
|
||||
},
|
||||
{} as Record<string, string>,
|
||||
)
|
||||
: undefined;
|
||||
|
||||
return this.firebaseService.sendNotification(tokens, title, body, stringData);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to send push notification to user ${userId}: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
// Don't throw - notification is already saved to DB
|
||||
}
|
||||
|
||||
// Convert data to string values (Firebase requires string values in data payload)
|
||||
const stringData: Record<string, string> | undefined = data
|
||||
? Object.entries(data).reduce(
|
||||
(acc, [key, value]) => {
|
||||
acc[key] = String(value);
|
||||
return acc;
|
||||
},
|
||||
{} as Record<string, string>,
|
||||
)
|
||||
: undefined;
|
||||
|
||||
return this.firebaseService.sendNotification(tokens, title, body, stringData);
|
||||
}
|
||||
|
||||
private async sendSMS(to: string, body: string) {
|
||||
|
||||
@ -0,0 +1,149 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { I18nService } from 'nestjs-i18n';
|
||||
import { NotificationFactory, NotificationPreferences } from '../services/notification-factory.service';
|
||||
import { UserService } from '~/user/services/user.service';
|
||||
import { NOTIFICATION_EVENTS } from '../constants/event-names.constant';
|
||||
import { IProfileUpdatedEvent } from '../interfaces/notification-events.interface';
|
||||
import { NotificationScope } from '../enums/notification-scope.enum';
|
||||
import { User } from '~/user/entities';
|
||||
import { UserLocale } from '~/core/enums/user-locale.enum';
|
||||
|
||||
@Injectable()
|
||||
export class ProfileNotificationListener {
|
||||
private readonly logger = new Logger(ProfileNotificationListener.name);
|
||||
|
||||
constructor(
|
||||
private readonly notificationFactory: NotificationFactory,
|
||||
private readonly userService: UserService,
|
||||
private readonly i18n: I18nService,
|
||||
) {}
|
||||
|
||||
@OnEvent(NOTIFICATION_EVENTS.PROFILE_UPDATED)
|
||||
async handleProfileUpdated(event: IProfileUpdatedEvent): Promise<void> {
|
||||
try {
|
||||
const { user, updatedFields } = event;
|
||||
|
||||
this.logger.log(
|
||||
`Processing profile updated notification for user ${user.id} - Updated fields: ${updatedFields.join(', ')}`
|
||||
);
|
||||
|
||||
await this.notifyUserOfProfileUpdate(user, updatedFields);
|
||||
|
||||
this.logger.log(
|
||||
`Profile updated notification processed successfully for user ${user.id}`
|
||||
);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to process profile updated notification: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async notifyUserOfProfileUpdate(user: any, updatedFields: string[]): Promise<void> {
|
||||
try {
|
||||
if (!user) {
|
||||
this.logger.warn(`No user found, skipping profile update notification`);
|
||||
return;
|
||||
}
|
||||
|
||||
const locale = this.getUserLocale(user);
|
||||
const isEmailUpdate = updatedFields.includes('email');
|
||||
const isPasswordUpdate = updatedFields.includes('password');
|
||||
const isProfilePictureUpdate = updatedFields.includes('profilePictureId');
|
||||
const isNameUpdate = updatedFields.includes('firstName') || updatedFields.includes('lastName');
|
||||
|
||||
let title: string;
|
||||
let message: string;
|
||||
|
||||
try {
|
||||
if (isEmailUpdate) {
|
||||
title = this.i18n.t('app.NOTIFICATION.PROFILE_EMAIL_UPDATED_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.PROFILE_EMAIL_UPDATED_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
email: user.email || 'your email',
|
||||
},
|
||||
});
|
||||
} else if (isPasswordUpdate) {
|
||||
title = this.i18n.t('app.NOTIFICATION.PROFILE_PASSWORD_UPDATED_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.PROFILE_PASSWORD_UPDATED_MESSAGE', {
|
||||
lang: locale,
|
||||
});
|
||||
} else if (isProfilePictureUpdate) {
|
||||
title = this.i18n.t('app.NOTIFICATION.PROFILE_PICTURE_UPDATED_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.PROFILE_PICTURE_UPDATED_MESSAGE', {
|
||||
lang: locale,
|
||||
});
|
||||
} else if (isNameUpdate) {
|
||||
title = this.i18n.t('app.NOTIFICATION.PROFILE_NAME_UPDATED_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.PROFILE_NAME_UPDATED_MESSAGE', {
|
||||
lang: locale,
|
||||
});
|
||||
} else {
|
||||
title = this.i18n.t('app.NOTIFICATION.PROFILE_UPDATED_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.PROFILE_UPDATED_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
fields: updatedFields.join(', '),
|
||||
},
|
||||
});
|
||||
}
|
||||
} catch (i18nError: any) {
|
||||
this.logger.error(
|
||||
`[ProfileNotificationListener] i18n error for user ${user.id}: ${i18nError?.message || 'Unknown i18n error'}. Falling back to English.`,
|
||||
i18nError?.stack
|
||||
);
|
||||
|
||||
if (isEmailUpdate) {
|
||||
title = 'Email Updated';
|
||||
message = `Your email has been updated to ${user.email || 'a new email'}. Please verify your new email address.`;
|
||||
} else if (isPasswordUpdate) {
|
||||
title = 'Password Updated';
|
||||
message = 'Your password has been successfully updated. If you did not make this change, please contact support immediately.';
|
||||
} else {
|
||||
title = 'Profile Updated';
|
||||
message = `Your profile has been updated. Changes: ${updatedFields.join(', ')}`;
|
||||
}
|
||||
}
|
||||
|
||||
this.logger.debug(
|
||||
`Notifying user (user ${user.id}): Profile updated - ${updatedFields.join(', ')}`
|
||||
);
|
||||
|
||||
await this.notificationFactory.send({
|
||||
userId: user.id,
|
||||
title,
|
||||
message,
|
||||
scope: NotificationScope.PROFILE_UPDATED,
|
||||
preferences: this.getUserPreferences(user),
|
||||
data: {
|
||||
updatedFields: updatedFields,
|
||||
timestamp: new Date().toISOString(),
|
||||
type: 'PROFILE_UPDATE',
|
||||
action: 'VIEW_PROFILE',
|
||||
},
|
||||
});
|
||||
|
||||
this.logger.log(`✅ Notified user ${user.id} about profile update`);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to notify user of profile update: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private getUserPreferences(user: User): NotificationPreferences {
|
||||
return {
|
||||
isPushEnabled: user.isPushEnabled,
|
||||
isEmailEnabled: user.isEmailEnabled,
|
||||
isSmsEnabled: user.isSmsEnabled,
|
||||
};
|
||||
}
|
||||
|
||||
private getUserLocale(user: User): UserLocale {
|
||||
return UserLocale.ENGLISH;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,282 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { I18nService } from 'nestjs-i18n';
|
||||
import { NotificationFactory, NotificationPreferences } from '../services/notification-factory.service';
|
||||
import { UserService } from '~/user/services/user.service';
|
||||
import { NOTIFICATION_EVENTS } from '../constants/event-names.constant';
|
||||
import {
|
||||
IMaintenanceAlertEvent,
|
||||
ISuspiciousLoginEvent,
|
||||
ITransactionFailedEvent,
|
||||
} from '../interfaces/notification-events.interface';
|
||||
import { NotificationScope } from '../enums/notification-scope.enum';
|
||||
import { User } from '~/user/entities';
|
||||
import { UserLocale } from '~/core/enums/user-locale.enum';
|
||||
|
||||
@Injectable()
|
||||
export class SystemAlertNotificationListener {
|
||||
private readonly logger = new Logger(SystemAlertNotificationListener.name);
|
||||
|
||||
constructor(
|
||||
private readonly notificationFactory: NotificationFactory,
|
||||
private readonly userService: UserService,
|
||||
private readonly i18n: I18nService,
|
||||
) {}
|
||||
|
||||
@OnEvent(NOTIFICATION_EVENTS.MAINTENANCE_ALERT)
|
||||
async handleMaintenanceAlert(event: IMaintenanceAlertEvent): Promise<void> {
|
||||
try {
|
||||
const { userId, message, startTime, endTime } = event;
|
||||
|
||||
this.logger.log(
|
||||
`Processing maintenance alert notification - User: ${userId || 'ALL'}, Message: ${message}`
|
||||
);
|
||||
|
||||
if (userId) {
|
||||
await this.notifyUserOfMaintenance(userId, message, startTime, endTime);
|
||||
} else {
|
||||
this.logger.warn('Broadcast maintenance alerts to all users not yet implemented');
|
||||
}
|
||||
|
||||
this.logger.log(`Maintenance alert notification processed successfully`);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to process maintenance alert notification: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@OnEvent(NOTIFICATION_EVENTS.TRANSACTION_FAILED)
|
||||
async handleTransactionFailed(event: ITransactionFailedEvent): Promise<void> {
|
||||
try {
|
||||
const { userId, transactionId, reason, amount } = event;
|
||||
|
||||
this.logger.log(
|
||||
`Processing transaction failed notification for user ${userId} - Transaction: ${transactionId}, Reason: ${reason}`
|
||||
);
|
||||
|
||||
await this.notifyUserOfTransactionFailure(userId, transactionId, reason, amount);
|
||||
|
||||
this.logger.log(`Transaction failed notification processed successfully for user ${userId}`);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to process transaction failed notification: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@OnEvent(NOTIFICATION_EVENTS.SUSPICIOUS_LOGIN)
|
||||
async handleSuspiciousLogin(event: ISuspiciousLoginEvent): Promise<void> {
|
||||
try {
|
||||
const { userId, ipAddress, location, device } = event;
|
||||
|
||||
this.logger.log(
|
||||
`Processing suspicious login notification for user ${userId} - IP: ${ipAddress}, Location: ${location}`
|
||||
);
|
||||
|
||||
await this.notifyUserOfSuspiciousLogin(userId, ipAddress, location, device);
|
||||
|
||||
this.logger.log(`Suspicious login notification processed successfully for user ${userId}`);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to process suspicious login notification: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async notifyUserOfMaintenance(
|
||||
userId: string,
|
||||
message: string,
|
||||
startTime?: Date,
|
||||
endTime?: Date,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const user = await this.userService.findUserOrThrow({ id: userId });
|
||||
const locale = this.getUserLocale(user);
|
||||
|
||||
let title: string;
|
||||
let notificationMessage: string;
|
||||
|
||||
try {
|
||||
title = this.i18n.t('app.NOTIFICATION.MAINTENANCE_ALERT_TITLE', { lang: locale });
|
||||
notificationMessage = this.i18n.t('app.NOTIFICATION.MAINTENANCE_ALERT_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
message: message,
|
||||
startTime: startTime ? startTime.toLocaleString() : '',
|
||||
endTime: endTime ? endTime.toLocaleString() : '',
|
||||
},
|
||||
});
|
||||
} catch (i18nError: any) {
|
||||
this.logger.error(
|
||||
`[SystemAlertNotificationListener] i18n error for user ${userId}: ${i18nError?.message || 'Unknown i18n error'}. Falling back to English.`,
|
||||
i18nError?.stack
|
||||
);
|
||||
title = 'Scheduled Maintenance';
|
||||
notificationMessage = message || 'The system will be under maintenance. Please check back later.';
|
||||
if (startTime && endTime) {
|
||||
notificationMessage += ` Scheduled from ${startTime.toLocaleString()} to ${endTime.toLocaleString()}.`;
|
||||
}
|
||||
}
|
||||
|
||||
await this.notificationFactory.send({
|
||||
userId: user.id,
|
||||
title,
|
||||
message: notificationMessage,
|
||||
scope: NotificationScope.MAINTENANCE_ALERT,
|
||||
preferences: this.getUserPreferences(user),
|
||||
data: {
|
||||
message: message,
|
||||
startTime: startTime?.toISOString(),
|
||||
endTime: endTime?.toISOString(),
|
||||
timestamp: new Date().toISOString(),
|
||||
type: 'MAINTENANCE',
|
||||
action: 'VIEW_STATUS',
|
||||
},
|
||||
});
|
||||
|
||||
this.logger.log(`✅ Notified user ${userId} about maintenance`);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to notify user of maintenance: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async notifyUserOfTransactionFailure(
|
||||
userId: string,
|
||||
transactionId: string | undefined,
|
||||
reason: string,
|
||||
amount?: number,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const user = await this.userService.findUserOrThrow({ id: userId });
|
||||
const locale = this.getUserLocale(user);
|
||||
|
||||
let title: string;
|
||||
let message: string;
|
||||
|
||||
try {
|
||||
title = this.i18n.t('app.NOTIFICATION.TRANSACTION_FAILED_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.TRANSACTION_FAILED_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
reason: reason,
|
||||
amount: amount ? amount.toString() : '',
|
||||
},
|
||||
});
|
||||
} catch (i18nError: any) {
|
||||
this.logger.error(
|
||||
`[SystemAlertNotificationListener] i18n error for user ${userId}: ${i18nError?.message || 'Unknown i18n error'}. Falling back to English.`,
|
||||
i18nError?.stack
|
||||
);
|
||||
title = 'Transaction Failed';
|
||||
message = `Your transaction could not be completed. Reason: ${reason}.`;
|
||||
if (amount) {
|
||||
message += ` Amount: ${amount}`;
|
||||
}
|
||||
message += ' Please try again or contact support if the issue persists.';
|
||||
}
|
||||
|
||||
await this.notificationFactory.send({
|
||||
userId: user.id,
|
||||
title,
|
||||
message,
|
||||
scope: NotificationScope.TRANSACTION_FAILED,
|
||||
preferences: this.getUserPreferences(user),
|
||||
data: {
|
||||
transactionId: transactionId,
|
||||
reason: reason,
|
||||
amount: amount,
|
||||
timestamp: new Date().toISOString(),
|
||||
type: 'TRANSACTION_FAILED',
|
||||
action: 'RETRY_TRANSACTION',
|
||||
},
|
||||
});
|
||||
|
||||
this.logger.log(`✅ Notified user ${userId} about failed transaction`);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to notify user of transaction failure: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async notifyUserOfSuspiciousLogin(
|
||||
userId: string,
|
||||
ipAddress?: string,
|
||||
location?: string,
|
||||
device?: string,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const user = await this.userService.findUserOrThrow({ id: userId });
|
||||
const locale = this.getUserLocale(user);
|
||||
|
||||
let title: string;
|
||||
let message: string;
|
||||
|
||||
try {
|
||||
title = this.i18n.t('app.NOTIFICATION.SUSPICIOUS_LOGIN_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.SUSPICIOUS_LOGIN_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
location: location || 'unknown location',
|
||||
device: device || 'unknown device',
|
||||
ipAddress: ipAddress || 'unknown IP',
|
||||
},
|
||||
});
|
||||
} catch (i18nError: any) {
|
||||
this.logger.error(
|
||||
`[SystemAlertNotificationListener] i18n error for user ${userId}: ${i18nError?.message || 'Unknown i18n error'}. Falling back to English.`,
|
||||
i18nError?.stack
|
||||
);
|
||||
title = 'Suspicious Login Detected';
|
||||
message = `We detected a login attempt from ${location || 'an unknown location'} (${ipAddress || 'unknown IP'})`;
|
||||
if (device) {
|
||||
message += ` using ${device}`;
|
||||
}
|
||||
message += '. If this was not you, please change your password immediately and contact support.';
|
||||
}
|
||||
|
||||
await this.notificationFactory.send({
|
||||
userId: user.id,
|
||||
title,
|
||||
message,
|
||||
scope: NotificationScope.SUSPICIOUS_LOGIN,
|
||||
preferences: this.getUserPreferences(user),
|
||||
data: {
|
||||
ipAddress: ipAddress,
|
||||
location: location,
|
||||
device: device,
|
||||
timestamp: new Date().toISOString(),
|
||||
type: 'SUSPICIOUS_LOGIN',
|
||||
action: 'CHANGE_PASSWORD',
|
||||
},
|
||||
});
|
||||
|
||||
this.logger.log(`✅ Notified user ${userId} about suspicious login`);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to notify user of suspicious login: ${error?.message || 'Unknown error'}`,
|
||||
error?.stack
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private getUserPreferences(user: User): NotificationPreferences {
|
||||
return {
|
||||
isPushEnabled: user.isPushEnabled,
|
||||
isEmailEnabled: user.isEmailEnabled,
|
||||
isSmsEnabled: user.isSmsEnabled,
|
||||
};
|
||||
}
|
||||
|
||||
private getUserLocale(user: User): UserLocale {
|
||||
return UserLocale.ENGLISH;
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,10 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { I18nService } from 'nestjs-i18n';
|
||||
import { NotificationFactory, NotificationPreferences } from '../services/notification-factory.service';
|
||||
import { UserService } from '~/user/services/user.service';
|
||||
import { AccountService } from '~/card/services/account.service';
|
||||
import { CardService } from '~/card/services/card.service';
|
||||
import { NOTIFICATION_EVENTS } from '../constants/event-names.constant';
|
||||
import { ITransactionCreatedEvent } from '../interfaces/notification-events.interface';
|
||||
import { NotificationScope } from '../enums/notification-scope.enum';
|
||||
@ -10,6 +12,7 @@ import { Transaction } from '~/card/entities/transaction.entity';
|
||||
import { Card } from '~/card/entities/card.entity';
|
||||
import { User } from '~/user/entities';
|
||||
import { UserLocale } from '~/core/enums/user-locale.enum';
|
||||
import { formatCurrencyAmount, getCurrency, numericToCurrencyCode } from '~/common/utils/currency.util';
|
||||
|
||||
/**
|
||||
* TransactionNotificationListener
|
||||
@ -32,6 +35,10 @@ export class TransactionNotificationListener {
|
||||
private readonly notificationFactory: NotificationFactory,
|
||||
private readonly userService: UserService,
|
||||
private readonly i18n: I18nService,
|
||||
@Inject(forwardRef(() => AccountService))
|
||||
private readonly accountService: AccountService,
|
||||
@Inject(forwardRef(() => CardService))
|
||||
private readonly cardService: CardService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@ -90,50 +97,173 @@ export class TransactionNotificationListener {
|
||||
return;
|
||||
}
|
||||
|
||||
const scope = isTopUp
|
||||
? NotificationScope.CHILD_TOP_UP
|
||||
: NotificationScope.CHILD_SPENDING;
|
||||
// Determine scope: internal transfer (parent to child) vs external top-up
|
||||
let scope: NotificationScope;
|
||||
if (isTopUp) {
|
||||
scope = isChildSpending
|
||||
? NotificationScope.CHILD_INTERNAL_TRANSFER // Parent transferring to child
|
||||
: NotificationScope.CHILD_TOP_UP; // External top-up
|
||||
} else {
|
||||
scope = NotificationScope.CHILD_SPENDING;
|
||||
}
|
||||
|
||||
const locale = this.getUserLocale(user);
|
||||
const amount = transaction.transactionAmount;
|
||||
const merchant = transaction.merchantName || 'merchant';
|
||||
const balance = card.account?.balance || 0;
|
||||
const currency = card.account?.currency || transaction.transactionCurrency || 'SAR';
|
||||
|
||||
// For child notifications, show the appropriate balance based on account structure
|
||||
let balance = 0;
|
||||
let accountCurrency: string | undefined;
|
||||
|
||||
if (isTopUp && isChildSpending) {
|
||||
// Internal transfer: For shared accounts, show card limit (child's spending power)
|
||||
// For separate accounts, show child's account balance
|
||||
try {
|
||||
// Reload card to get updated data
|
||||
const cardWithUpdatedBalance = await this.cardService.getCardById(card.id);
|
||||
|
||||
// Check if child has parent (shared account scenario)
|
||||
if (cardWithUpdatedBalance.parentId) {
|
||||
// Likely shared account - use card limit as the child's "balance"
|
||||
balance = cardWithUpdatedBalance.limit || card.limit || 0;
|
||||
accountCurrency = cardWithUpdatedBalance.account?.currency || card.account?.currency;
|
||||
this.logger.debug(
|
||||
`[Child Internal Transfer] Shared account - using card limit: ${balance} ${accountCurrency}`
|
||||
);
|
||||
} else {
|
||||
// Separate account - use child's account balance
|
||||
if (cardWithUpdatedBalance?.account?.accountReference) {
|
||||
const account = await this.accountService.getAccountByReferenceNumber(
|
||||
cardWithUpdatedBalance.account.accountReference
|
||||
);
|
||||
balance = account.balance;
|
||||
accountCurrency = account.currency;
|
||||
this.logger.debug(
|
||||
`[Child Internal Transfer] Separate account - using account balance: ${balance} ${accountCurrency}`
|
||||
);
|
||||
} else {
|
||||
balance = cardWithUpdatedBalance.account?.balance || card.account?.balance || 0;
|
||||
accountCurrency = cardWithUpdatedBalance.account?.currency || card.account?.currency;
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
this.logger.warn(
|
||||
`[Child Internal Transfer] Could not fetch balance: ${error?.message}. Using card limit.`
|
||||
);
|
||||
balance = card.limit || 0;
|
||||
accountCurrency = card.account?.currency;
|
||||
}
|
||||
} else if (isTopUp) {
|
||||
// External top-up: show child's account balance
|
||||
try {
|
||||
const cardWithUpdatedBalance = await this.cardService.getCardById(card.id);
|
||||
if (cardWithUpdatedBalance?.account?.accountReference) {
|
||||
const account = await this.accountService.getAccountByReferenceNumber(
|
||||
cardWithUpdatedBalance.account.accountReference
|
||||
);
|
||||
balance = account.balance;
|
||||
accountCurrency = account.currency;
|
||||
this.logger.debug(
|
||||
`[Child Top-Up Notification] Fetched account by reference - balance: ${balance} ${accountCurrency}`
|
||||
);
|
||||
} else {
|
||||
balance = cardWithUpdatedBalance.account?.balance || card.account?.balance || 0;
|
||||
accountCurrency = cardWithUpdatedBalance.account?.currency || card.account?.currency;
|
||||
}
|
||||
} catch (error: any) {
|
||||
this.logger.warn(
|
||||
`[Child Top-Up Notification] Could not fetch account: ${error?.message}. Using card balance.`
|
||||
);
|
||||
balance = card.account?.balance || 0;
|
||||
accountCurrency = card.account?.currency;
|
||||
}
|
||||
} else {
|
||||
// For spending: show account balance
|
||||
try {
|
||||
// Reload card to get account reference
|
||||
const cardWithUpdatedBalance = await this.cardService.getCardById(card.id);
|
||||
if (cardWithUpdatedBalance?.account?.accountReference) {
|
||||
// Fetch by reference number to get fresh balance from database
|
||||
const account = await this.accountService.getAccountByReferenceNumber(
|
||||
cardWithUpdatedBalance.account.accountReference
|
||||
);
|
||||
balance = account.balance;
|
||||
accountCurrency = account.currency;
|
||||
this.logger.debug(
|
||||
`[Child Spending Notification] Fetched account by reference - balance: ${balance} ${accountCurrency}`
|
||||
);
|
||||
} else {
|
||||
// Fallback: use card's account balance
|
||||
balance = cardWithUpdatedBalance.account?.balance || card.account?.balance || 0;
|
||||
accountCurrency = cardWithUpdatedBalance.account?.currency || card.account?.currency;
|
||||
this.logger.debug(
|
||||
`[Child Spending Notification] Using card account balance - balance: ${balance} ${accountCurrency}`
|
||||
);
|
||||
}
|
||||
} catch (error: any) {
|
||||
this.logger.warn(
|
||||
`[Child Spending Notification] Could not fetch account by reference: ${error?.message}. Using card account balance.`
|
||||
);
|
||||
// Fallback: use card's account balance
|
||||
balance = card.account?.balance || 0;
|
||||
accountCurrency = card.account?.currency;
|
||||
}
|
||||
}
|
||||
const currency = getCurrency(
|
||||
accountCurrency,
|
||||
transaction.transactionCurrency,
|
||||
'SAR'
|
||||
);
|
||||
|
||||
this.logger.debug(
|
||||
`[Child Notification] Account currency: ${accountCurrency}, Transaction currency: ${transaction.transactionCurrency}, Final currency: ${currency}, Balance: ${balance}, Amount: ${amount}`
|
||||
);
|
||||
|
||||
const formattedAmount = formatCurrencyAmount(amount, currency);
|
||||
const formattedBalance = formatCurrencyAmount(balance, currency);
|
||||
|
||||
let title: string;
|
||||
let message: string;
|
||||
|
||||
try {
|
||||
title = isTopUp
|
||||
? this.i18n.t('app.NOTIFICATION.CHILD_TOP_UP_TITLE', { lang: locale })
|
||||
: this.i18n.t('app.NOTIFICATION.CHILD_SPENDING_TITLE', { lang: locale });
|
||||
|
||||
message = isTopUp
|
||||
? this.i18n.t('app.NOTIFICATION.CHILD_TOP_UP_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
amount: amount.toString(),
|
||||
currency: currency,
|
||||
balance: balance.toString(),
|
||||
},
|
||||
})
|
||||
: this.i18n.t('app.NOTIFICATION.CHILD_SPENDING_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
amount: amount.toString(),
|
||||
currency: currency,
|
||||
merchant: merchant,
|
||||
balance: balance.toString(),
|
||||
},
|
||||
});
|
||||
if (isTopUp) {
|
||||
// Internal transfer or external top-up
|
||||
const titleKey = isChildSpending
|
||||
? 'app.NOTIFICATION.CHILD_INTERNAL_TRANSFER_TITLE'
|
||||
: 'app.NOTIFICATION.CHILD_TOP_UP_TITLE';
|
||||
const messageKey = isChildSpending
|
||||
? 'app.NOTIFICATION.CHILD_INTERNAL_TRANSFER_MESSAGE'
|
||||
: 'app.NOTIFICATION.CHILD_TOP_UP_MESSAGE';
|
||||
|
||||
title = this.i18n.t(titleKey, { lang: locale });
|
||||
message = this.i18n.t(messageKey, {
|
||||
lang: locale,
|
||||
args: {
|
||||
amount: formattedAmount,
|
||||
currency: currency,
|
||||
balance: formattedBalance,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// Spending
|
||||
title = this.i18n.t('app.NOTIFICATION.CHILD_SPENDING_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.CHILD_SPENDING_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
amount: formattedAmount,
|
||||
currency: currency,
|
||||
merchant: merchant,
|
||||
},
|
||||
});
|
||||
}
|
||||
} catch (i18nError: any) {
|
||||
console.error(`[TransactionNotificationListener] i18n error:`, i18nError);
|
||||
this.logger.error(`i18n translation failed: ${i18nError?.message}`, i18nError?.stack);
|
||||
// Fallback to English without i18n
|
||||
title = isTopUp ? 'Card Topped Up' : 'Purchase Successful';
|
||||
title = isTopUp ? 'Funds Credited' : 'Purchase Successful';
|
||||
message = isTopUp
|
||||
? `You received ${amount} ${currency}. Total balance: ${balance} ${currency}`
|
||||
: `You spent ${amount} ${currency} at ${merchant}. Balance: ${balance} ${currency}`;
|
||||
? `${formattedAmount} ${currency} has been added to your card. Total balance: ${formattedBalance} ${currency}`
|
||||
: `You spent ${formattedAmount} ${currency} at ${merchant}`;
|
||||
}
|
||||
|
||||
this.logger.debug(
|
||||
@ -146,17 +276,17 @@ export class TransactionNotificationListener {
|
||||
message,
|
||||
scope,
|
||||
preferences: this.getUserPreferences(user),
|
||||
data: {
|
||||
transactionId: transaction.id,
|
||||
amount: amount.toString(),
|
||||
currency: currency,
|
||||
merchant: merchant,
|
||||
merchantCategory: transaction.merchantCategoryCode || 'OTHER',
|
||||
balance: balance.toString(),
|
||||
timestamp: transaction.transactionDate.toISOString(),
|
||||
type: isTopUp ? 'TOP_UP' : 'SPENDING',
|
||||
action: 'OPEN_TRANSACTION',
|
||||
},
|
||||
data: {
|
||||
transactionId: transaction.id,
|
||||
amount: formattedAmount,
|
||||
currency: currency, // ISO currency code (SAR, USD, etc.)
|
||||
merchant: merchant,
|
||||
merchantCategory: transaction.merchantCategoryCode || 'OTHER',
|
||||
balance: formattedBalance,
|
||||
timestamp: transaction.transactionDate.toISOString(),
|
||||
type: isTopUp ? 'TOP_UP' : 'SPENDING',
|
||||
action: 'OPEN_TRANSACTION',
|
||||
},
|
||||
});
|
||||
|
||||
this.logger.log(`✅ Notified user ${user.id} for transaction ${transaction.id}`);
|
||||
@ -190,11 +320,101 @@ export class TransactionNotificationListener {
|
||||
const childName = childUser?.firstName || defaultChildName;
|
||||
const amount = transaction.transactionAmount;
|
||||
const merchant = transaction.merchantName || 'a merchant';
|
||||
const balance = card.account?.balance || 0;
|
||||
const currency = card.account?.currency || transaction.transactionCurrency || 'SAR';
|
||||
|
||||
// Get parent's available balance (balance - reserved_balance) - reload to get fresh balance
|
||||
let parentAccountBalance = 0;
|
||||
let parentAccountReservedBalance = 0;
|
||||
let parentAccountCurrency: string | undefined;
|
||||
let availableBalance = 0;
|
||||
|
||||
try {
|
||||
if (card.parentId) {
|
||||
// Get parent's card to access their account reference
|
||||
const parentCard = await this.cardService.getCardByCustomerId(card.parentId);
|
||||
if (parentCard?.account?.accountReference) {
|
||||
// Fetch by reference number to get fresh balance from database
|
||||
const parentAccount = await this.accountService.getAccountByReferenceNumber(
|
||||
parentCard.account.accountReference
|
||||
);
|
||||
parentAccountBalance = parentAccount.balance;
|
||||
parentAccountReservedBalance = parentAccount.reservedBalance;
|
||||
availableBalance = parentAccountBalance - parentAccountReservedBalance;
|
||||
parentAccountCurrency = parentAccount.currency;
|
||||
this.logger.debug(
|
||||
`[Parent Spending] Fetched parent account by reference - balance: ${parentAccountBalance}, reserved: ${parentAccountReservedBalance}, available: ${availableBalance} ${parentAccountCurrency}`
|
||||
);
|
||||
} else {
|
||||
// Fallback: try by customer ID
|
||||
const parentAccount = await this.accountService.getAccountByCustomerId(card.parentId);
|
||||
parentAccountBalance = parentAccount.balance;
|
||||
parentAccountReservedBalance = parentAccount.reservedBalance;
|
||||
availableBalance = parentAccountBalance - parentAccountReservedBalance;
|
||||
parentAccountCurrency = parentAccount.currency;
|
||||
this.logger.debug(
|
||||
`[Parent Spending] Fetched parent account by customer ID - balance: ${parentAccountBalance}, reserved: ${parentAccountReservedBalance}, available: ${availableBalance} ${parentAccountCurrency}`
|
||||
);
|
||||
}
|
||||
} else {
|
||||
const parentCustomer = customer?.junior?.guardian?.customer;
|
||||
if (parentCustomer?.id) {
|
||||
try {
|
||||
const parentCard = await this.cardService.getCardByCustomerId(parentCustomer.id);
|
||||
if (parentCard?.account?.accountReference) {
|
||||
const parentAccount = await this.accountService.getAccountByReferenceNumber(
|
||||
parentCard.account.accountReference
|
||||
);
|
||||
parentAccountBalance = parentAccount.balance;
|
||||
parentAccountReservedBalance = parentAccount.reservedBalance;
|
||||
availableBalance = parentAccountBalance - parentAccountReservedBalance;
|
||||
parentAccountCurrency = parentAccount.currency;
|
||||
this.logger.debug(
|
||||
`[Parent Spending] Fetched parent account via customer relation (by reference) - balance: ${parentAccountBalance}, reserved: ${parentAccountReservedBalance}, available: ${availableBalance} ${parentAccountCurrency}`
|
||||
);
|
||||
} else {
|
||||
const parentAccount = await this.accountService.getAccountByCustomerId(parentCustomer.id);
|
||||
parentAccountBalance = parentAccount.balance;
|
||||
parentAccountReservedBalance = parentAccount.reservedBalance;
|
||||
availableBalance = parentAccountBalance - parentAccountReservedBalance;
|
||||
parentAccountCurrency = parentAccount.currency;
|
||||
this.logger.debug(
|
||||
`[Parent Spending] Fetched parent account via customer relation - balance: ${parentAccountBalance}, reserved: ${parentAccountReservedBalance}, available: ${availableBalance} ${parentAccountCurrency}`
|
||||
);
|
||||
}
|
||||
} catch (error: any) {
|
||||
this.logger.warn(
|
||||
`[Parent Spending] Could not fetch parent account via customer: ${error?.message}. Using child account balance as fallback.`
|
||||
);
|
||||
availableBalance = card.account?.balance || 0;
|
||||
parentAccountCurrency = card.account?.currency;
|
||||
}
|
||||
} else {
|
||||
availableBalance = card.account?.balance || 0;
|
||||
parentAccountCurrency = card.account?.currency;
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
this.logger.warn(`[Parent Spending] Could not fetch parent account: ${error?.message}, using child account balance as fallback`);
|
||||
availableBalance = card.account?.balance || 0;
|
||||
parentAccountCurrency = card.account?.currency;
|
||||
}
|
||||
|
||||
const accountCurrency = parentAccountCurrency || card.account?.currency;
|
||||
const currency = getCurrency(
|
||||
accountCurrency,
|
||||
transaction.transactionCurrency,
|
||||
'SAR'
|
||||
);
|
||||
|
||||
this.logger.debug(
|
||||
`[Parent Spending Notification] Parent account currency: ${parentAccountCurrency}, Account currency: ${accountCurrency}, Transaction currency: ${transaction.transactionCurrency}, Final currency: ${currency}, Parent available balance: ${availableBalance}, Amount: ${amount}`
|
||||
);
|
||||
|
||||
const formattedAmount = formatCurrencyAmount(amount, currency);
|
||||
// Use available balance for parent spending notification
|
||||
const formattedBalance = formatCurrencyAmount(availableBalance, currency);
|
||||
|
||||
this.logger.debug(
|
||||
`Notifying parent (user ${parentUser.id}): ${childName} spent ${amount} ${currency} at ${merchant}`
|
||||
`Notifying parent (user ${parentUser.id}): ${childName} spent ${formattedAmount} ${currency} at ${merchant}`
|
||||
);
|
||||
|
||||
let title: string;
|
||||
@ -202,21 +422,21 @@ export class TransactionNotificationListener {
|
||||
|
||||
try {
|
||||
title = this.i18n.t('app.NOTIFICATION.PARENT_SPENDING_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.PARENT_SPENDING_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
childName: childName,
|
||||
amount: amount.toString(),
|
||||
currency: currency,
|
||||
merchant: merchant,
|
||||
balance: balance.toString(),
|
||||
},
|
||||
});
|
||||
message = this.i18n.t('app.NOTIFICATION.PARENT_SPENDING_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
childName: childName,
|
||||
amount: formattedAmount,
|
||||
currency: currency,
|
||||
merchant: merchant,
|
||||
balance: formattedBalance,
|
||||
},
|
||||
});
|
||||
} catch (i18nError: any) {
|
||||
console.error(`[TransactionNotificationListener] i18n error in parent spending:`, i18nError);
|
||||
this.logger.error(`i18n translation failed: ${i18nError?.message}`, i18nError?.stack);
|
||||
title = 'Child Spending Alert';
|
||||
message = `${childName} spent ${amount} ${currency} at ${merchant}. Balance: ${balance} ${currency}`;
|
||||
title = 'Spending Alert';
|
||||
message = `${childName} spent ${formattedAmount} ${currency} at ${merchant}. Remaining balance: ${formattedBalance} ${currency}`;
|
||||
}
|
||||
|
||||
await this.notificationFactory.send({
|
||||
@ -229,11 +449,11 @@ export class TransactionNotificationListener {
|
||||
transactionId: transaction.id,
|
||||
childId: childUser.id,
|
||||
childName: childName,
|
||||
amount: amount.toString(),
|
||||
currency: currency,
|
||||
amount: formattedAmount, // Use formatted amount instead of raw amount
|
||||
currency: currency, // ISO currency code (SAR, USD, etc.)
|
||||
merchant: merchant,
|
||||
merchantCategory: transaction.merchantCategoryCode || 'OTHER',
|
||||
balance: balance.toString(),
|
||||
balance: formattedBalance,
|
||||
timestamp: transaction.transactionDate.toISOString(),
|
||||
type: 'CHILD_SPENDING',
|
||||
action: 'OPEN_TRANSACTION',
|
||||
@ -250,7 +470,7 @@ export class TransactionNotificationListener {
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify parent when they top up their child's card
|
||||
* Notify parent when they transfer money to their child's card (internal transfer)
|
||||
* This is a confirmation notification for the parent
|
||||
*/
|
||||
private async notifyParentOfTopUp(transaction: Transaction, card: Card): Promise<void> {
|
||||
@ -270,47 +490,142 @@ export class TransactionNotificationListener {
|
||||
const defaultChildName = this.i18n.t('app.NOTIFICATION.YOUR_CHILD', { lang: locale });
|
||||
const childName = childUser?.firstName || defaultChildName;
|
||||
const amount = transaction.transactionAmount;
|
||||
const balance = card.account?.balance || 0;
|
||||
const currency = card.account?.currency || transaction.transactionCurrency || 'SAR';
|
||||
|
||||
// Fetch parent account by reference number to get fresh balance (bypasses entity cache)
|
||||
// For parent notification, show available_balance = balance - reserved_balance
|
||||
let parentAccountBalance = 0;
|
||||
let parentAccountReservedBalance = 0;
|
||||
let parentAccountCurrency: string | undefined;
|
||||
let availableBalance = 0;
|
||||
|
||||
if (card.parentId) {
|
||||
try {
|
||||
// Get parent's card to access their account reference
|
||||
// card.parentId is the parent's CUSTOMER ID
|
||||
const parentCard = await this.cardService.getCardByCustomerId(card.parentId);
|
||||
if (parentCard?.account?.accountReference) {
|
||||
// Fetch by reference number to get fresh balance from database
|
||||
const parentAccount = await this.accountService.getAccountByReferenceNumber(
|
||||
parentCard.account.accountReference
|
||||
);
|
||||
parentAccountBalance = parentAccount.balance;
|
||||
parentAccountReservedBalance = parentAccount.reservedBalance;
|
||||
availableBalance = parentAccountBalance - parentAccountReservedBalance;
|
||||
parentAccountCurrency = parentAccount.currency;
|
||||
this.logger.debug(
|
||||
`[Parent Top-Up] Fetched parent account by reference - balance: ${parentAccountBalance}, reserved: ${parentAccountReservedBalance}, available: ${availableBalance} ${parentAccountCurrency}`
|
||||
);
|
||||
} else {
|
||||
// Fallback: try by customer ID
|
||||
const parentAccount = await this.accountService.getAccountByCustomerId(card.parentId);
|
||||
parentAccountBalance = parentAccount.balance;
|
||||
parentAccountReservedBalance = parentAccount.reservedBalance;
|
||||
availableBalance = parentAccountBalance - parentAccountReservedBalance;
|
||||
parentAccountCurrency = parentAccount.currency;
|
||||
this.logger.debug(
|
||||
`[Parent Top-Up] Fetched parent account by customer ID - balance: ${parentAccountBalance}, reserved: ${parentAccountReservedBalance}, available: ${availableBalance} ${parentAccountCurrency}`
|
||||
);
|
||||
}
|
||||
} catch (error: any) {
|
||||
this.logger.warn(
|
||||
`[Parent Top-Up] Could not fetch parent account for customer ${card.parentId}: ${error?.message}. Using child account balance as fallback.`
|
||||
);
|
||||
availableBalance = card.account?.balance || 0;
|
||||
parentAccountCurrency = card.account?.currency;
|
||||
}
|
||||
} else {
|
||||
// If no parentId, try via customer relation
|
||||
const parentCustomer = customer?.junior?.guardian?.customer;
|
||||
if (parentCustomer?.id) {
|
||||
try {
|
||||
const parentCard = await this.cardService.getCardByCustomerId(parentCustomer.id);
|
||||
if (parentCard?.account?.accountReference) {
|
||||
const parentAccount = await this.accountService.getAccountByReferenceNumber(
|
||||
parentCard.account.accountReference
|
||||
);
|
||||
parentAccountBalance = parentAccount.balance;
|
||||
parentAccountReservedBalance = parentAccount.reservedBalance;
|
||||
availableBalance = parentAccountBalance - parentAccountReservedBalance;
|
||||
parentAccountCurrency = parentAccount.currency;
|
||||
this.logger.debug(
|
||||
`[Parent Top-Up] Fetched parent account via customer relation (by reference) - balance: ${parentAccountBalance}, reserved: ${parentAccountReservedBalance}, available: ${availableBalance} ${parentAccountCurrency}`
|
||||
);
|
||||
} else {
|
||||
const parentAccount = await this.accountService.getAccountByCustomerId(parentCustomer.id);
|
||||
parentAccountBalance = parentAccount.balance;
|
||||
parentAccountReservedBalance = parentAccount.reservedBalance;
|
||||
availableBalance = parentAccountBalance - parentAccountReservedBalance;
|
||||
parentAccountCurrency = parentAccount.currency;
|
||||
this.logger.debug(
|
||||
`[Parent Top-Up] Fetched parent account via customer relation - balance: ${parentAccountBalance}, reserved: ${parentAccountReservedBalance}, available: ${availableBalance} ${parentAccountCurrency}`
|
||||
);
|
||||
}
|
||||
} catch (error: any) {
|
||||
this.logger.warn(
|
||||
`[Parent Top-Up] Could not fetch parent account via customer: ${error?.message}. Using child account balance as fallback.`
|
||||
);
|
||||
availableBalance = card.account?.balance || 0;
|
||||
parentAccountCurrency = card.account?.currency;
|
||||
}
|
||||
} else {
|
||||
availableBalance = card.account?.balance || 0;
|
||||
parentAccountCurrency = card.account?.currency;
|
||||
}
|
||||
}
|
||||
|
||||
// Use available_balance for parent notification (balance - reserved_balance)
|
||||
const balance = availableBalance;
|
||||
const accountCurrency = parentAccountCurrency;
|
||||
const currency = getCurrency(
|
||||
accountCurrency,
|
||||
transaction.transactionCurrency,
|
||||
);
|
||||
|
||||
this.logger.debug(
|
||||
`[Parent Top-Up Notification] Parent account currency: ${parentAccountCurrency}, Account currency: ${accountCurrency}, Transaction currency: ${transaction.transactionCurrency}, Final currency: ${currency}, Parent balance: ${balance}, Amount: ${amount}`
|
||||
);
|
||||
|
||||
const formattedAmount = formatCurrencyAmount(amount, currency);
|
||||
const formattedBalance = formatCurrencyAmount(balance, currency);
|
||||
|
||||
this.logger.debug(
|
||||
`Notifying parent (user ${parentUser.id}): Transferred ${amount} ${currency} to ${childName}`
|
||||
`Notifying parent (user ${parentUser.id}): Transferred ${formattedAmount} ${currency} to ${childName}, child balance: ${formattedBalance} ${currency}`
|
||||
);
|
||||
|
||||
let title: string;
|
||||
let message: string;
|
||||
|
||||
try {
|
||||
title = this.i18n.t('app.NOTIFICATION.PARENT_TOP_UP_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.PARENT_TOP_UP_MESSAGE', {
|
||||
title = this.i18n.t('app.NOTIFICATION.PARENT_INTERNAL_TRANSFER_TITLE', { lang: locale });
|
||||
message = this.i18n.t('app.NOTIFICATION.PARENT_INTERNAL_TRANSFER_MESSAGE', {
|
||||
lang: locale,
|
||||
args: {
|
||||
amount: amount.toString(),
|
||||
amount: formattedAmount,
|
||||
currency: currency,
|
||||
childName: childName,
|
||||
balance: balance.toString(),
|
||||
balance: formattedBalance,
|
||||
},
|
||||
});
|
||||
} catch (i18nError: any) {
|
||||
console.error(`[TransactionNotificationListener] i18n error in parent top-up:`, i18nError);
|
||||
console.error(`[TransactionNotificationListener] i18n error in parent internal transfer:`, i18nError);
|
||||
this.logger.error(`i18n translation failed: ${i18nError?.message}`, i18nError?.stack);
|
||||
title = 'Top-Up Confirmation';
|
||||
message = `You transferred ${amount} ${currency} to ${childName}. Balance: ${balance} ${currency}`;
|
||||
title = 'Internal Transfer Completed';
|
||||
message = `${formattedAmount} ${currency} has been transferred to ${childName}'s card. ${childName}'s balance is ${formattedBalance} ${currency}`;
|
||||
}
|
||||
|
||||
await this.notificationFactory.send({
|
||||
userId: parentUser.id,
|
||||
title,
|
||||
message,
|
||||
scope: NotificationScope.PARENT_TOP_UP_CONFIRMATION,
|
||||
scope: NotificationScope.PARENT_INTERNAL_TRANSFER,
|
||||
preferences: this.getUserPreferences(parentUser),
|
||||
data: {
|
||||
transactionId: transaction.id,
|
||||
childId: childUser.id,
|
||||
childName: childName,
|
||||
amount: amount.toString(),
|
||||
currency: currency,
|
||||
balance: balance.toString(),
|
||||
amount: formattedAmount,
|
||||
currency: currency, // ISO currency code (SAR, USD, etc.)
|
||||
balance: formattedBalance,
|
||||
timestamp: transaction.transactionDate.toISOString(),
|
||||
type: 'TOP_UP',
|
||||
action: 'OPEN_TRANSACTION',
|
||||
|
||||
@ -3,14 +3,19 @@ import { forwardRef, Module } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { TwilioModule } from 'nestjs-twilio';
|
||||
import { CardModule } from '~/card/card.module';
|
||||
import { RedisModule } from '~/common/redis/redis.module';
|
||||
import { buildMailerOptions, buildTwilioOptions } from '~/core/module-options';
|
||||
import { UserModule } from '~/user/user.module';
|
||||
import { NotificationsController } from './controllers';
|
||||
import { Notification } from './entities';
|
||||
import {
|
||||
CardNotificationListener,
|
||||
KycNotificationListener,
|
||||
MoneyRequestNotificationListener,
|
||||
NotificationCreatedListener,
|
||||
ProfileNotificationListener,
|
||||
SystemAlertNotificationListener,
|
||||
TransactionNotificationListener,
|
||||
} from './listeners';
|
||||
import { NotificationsRepository } from './repositories';
|
||||
@ -21,6 +26,7 @@ import { MessagingSystemFactory, RedisPubSubMessagingService } from './services/
|
||||
imports: [
|
||||
forwardRef(() => RedisModule.register()),
|
||||
forwardRef(() => UserModule),
|
||||
forwardRef(() => CardModule),
|
||||
TypeOrmModule.forFeature([Notification]),
|
||||
TwilioModule.forRootAsync({
|
||||
useFactory: buildTwilioOptions,
|
||||
@ -40,6 +46,10 @@ import { MessagingSystemFactory, RedisPubSubMessagingService } from './services/
|
||||
NotificationCreatedListener,
|
||||
TransactionNotificationListener,
|
||||
MoneyRequestNotificationListener,
|
||||
KycNotificationListener,
|
||||
CardNotificationListener,
|
||||
ProfileNotificationListener,
|
||||
SystemAlertNotificationListener,
|
||||
RedisPubSubMessagingService,
|
||||
MessagingSystemFactory,
|
||||
],
|
||||
|
||||
@ -83,6 +83,10 @@ export class NotificationFactory {
|
||||
* Send a notification to a user
|
||||
* Routes to enabled channels based on provided preferences
|
||||
*
|
||||
* Note: Notifications are always saved to the database (via PUSH channel)
|
||||
* for history/audit purposes, even if push notifications are disabled.
|
||||
* The preferences only control whether push/email/SMS are actually sent.
|
||||
*
|
||||
* @param payload - Notification payload including preferences
|
||||
*/
|
||||
async send(payload: NotificationPayload): Promise<void> {
|
||||
@ -97,17 +101,23 @@ export class NotificationFactory {
|
||||
|
||||
const promises: Promise<any>[] = [];
|
||||
|
||||
if (preferences.isPushEnabled) {
|
||||
this.logger.debug(`Routing to PUSH channel for user ${payload.userId}`);
|
||||
promises.push(
|
||||
this.sendToChannel(payload, NotificationChannel.PUSH)
|
||||
);
|
||||
}
|
||||
// Always create notification record in database (via PUSH channel for storage)
|
||||
// This ensures notifications are saved for history, even if push is disabled
|
||||
this.logger.debug(`Creating notification record for user ${payload.userId}`);
|
||||
promises.push(
|
||||
this.sendToChannel(payload, NotificationChannel.PUSH)
|
||||
);
|
||||
|
||||
// Only send via additional channels if enabled
|
||||
// Note: PUSH channel is already added above for database storage
|
||||
// The actual push delivery will check preferences in FirebaseService
|
||||
|
||||
await Promise.all(promises);
|
||||
|
||||
const activeChannels = preferences.isPushEnabled ? 1 : 0;
|
||||
this.logger.log(
|
||||
`Notification sent to user ${payload.userId} via ${promises.length} channel(s)`
|
||||
`Notification sent to user ${payload.userId} via ${activeChannels} active channel(s) ` +
|
||||
`(saved to database regardless of preferences)`
|
||||
);
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
|
||||
@ -6,7 +6,7 @@ import { OtpType } from '../../otp/enums';
|
||||
import { ISendOtp } from '../../otp/interfaces';
|
||||
import { SendEmailRequestDto } from '../dtos/request';
|
||||
import { Notification } from '../entities';
|
||||
import { EventType, NotificationChannel, NotificationScope } from '../enums';
|
||||
import { EventType, NotificationChannel, NotificationScope, NotificationStatus } from '../enums';
|
||||
import { NotificationsRepository } from '../repositories';
|
||||
import { MessagingSystemFactory } from './messaging/messaging-system-factory.service';
|
||||
|
||||
@ -35,7 +35,10 @@ export class NotificationsService {
|
||||
|
||||
async createNotification(notification: Partial<Notification>) {
|
||||
this.logger.log(`Creating notification for user ${notification.userId}`);
|
||||
const savedNotification = await this.notificationRepository.createNotification(notification);
|
||||
const savedNotification = await this.notificationRepository.createNotification({
|
||||
...notification,
|
||||
status: notification.status || NotificationStatus.UNREAD,
|
||||
});
|
||||
|
||||
const scope = notification.scope || NotificationScope.USER_REGISTERED;
|
||||
const messagingSystem = this.messagingSystemFactory.getMessagingSystem(scope);
|
||||
|
||||
@ -14,7 +14,15 @@ export class RedisModule {
|
||||
{
|
||||
provide: 'REDIS_PUBLISHER',
|
||||
useFactory: async (configService: ConfigService) => {
|
||||
const publisher = createClient({ url: configService.get<string>('REDIS_URL') });
|
||||
// Skip Redis connection during migration generation
|
||||
if (process.env.MIGRATIONS_RUN === 'false') {
|
||||
return null;
|
||||
}
|
||||
const redisUrl = configService.get<string>('REDIS_URL');
|
||||
if (!redisUrl) {
|
||||
return null;
|
||||
}
|
||||
const publisher = createClient({ url: redisUrl });
|
||||
await publisher.connect();
|
||||
return publisher;
|
||||
},
|
||||
@ -24,7 +32,15 @@ export class RedisModule {
|
||||
{
|
||||
provide: 'REDIS_SUBSCRIBER',
|
||||
useFactory: async (configService: ConfigService) => {
|
||||
const subscriber = createClient({ url: configService.get<string>('REDIS_URL') });
|
||||
// Skip Redis connection during migration generation
|
||||
if (process.env.MIGRATIONS_RUN === 'false') {
|
||||
return null;
|
||||
}
|
||||
const redisUrl = configService.get<string>('REDIS_URL');
|
||||
if (!redisUrl) {
|
||||
return null;
|
||||
}
|
||||
const subscriber = createClient({ url: redisUrl });
|
||||
await subscriber.connect();
|
||||
return subscriber;
|
||||
},
|
||||
|
||||
@ -15,6 +15,10 @@ export class RedisPubSubService implements OnModuleInit {
|
||||
) {}
|
||||
|
||||
onModuleInit() {
|
||||
// Skip subscription during migration generation
|
||||
if (process.env.MIGRATIONS_RUN === 'false' || !this.subscriber) {
|
||||
return;
|
||||
}
|
||||
this.subscriber.subscribe(EventType.NOTIFICATION_CREATED, async (message) => {
|
||||
const data = JSON.parse(message);
|
||||
this.logger.log('Received message on NOTIFICATION_CREATED channel:', data);
|
||||
|
||||
111
src/common/utils/currency.util.ts
Normal file
111
src/common/utils/currency.util.ts
Normal file
@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Currency utility functions
|
||||
* Handles currency code mapping and formatting
|
||||
*/
|
||||
|
||||
/**
|
||||
* ISO 4217 numeric currency codes to ISO currency code mapping
|
||||
* Common codes used in the system:
|
||||
* - 682: SAR (Saudi Riyal)
|
||||
* - 900: USD (US Dollar) - if used
|
||||
* - 784: AED (UAE Dirham)
|
||||
* - 414: KWD (Kuwaiti Dinar)
|
||||
* - 512: OMR (Omani Rial)
|
||||
* - 048: BHD (Bahraini Dinar)
|
||||
* - 400: JOD (Jordanian Dinar)
|
||||
*/
|
||||
export const NUMERIC_TO_CURRENCY_CODE: Record<string, string> = {
|
||||
'682': 'SAR',
|
||||
'900': 'USD',
|
||||
'784': 'AED',
|
||||
'414': 'KWD',
|
||||
'512': 'OMR',
|
||||
'048': 'BHD',
|
||||
'400': 'JOD',
|
||||
'586': 'PKR',
|
||||
};
|
||||
|
||||
/**
|
||||
* Currency decimal places mapping
|
||||
* ISO 4217 standard decimal places for each currency
|
||||
*/
|
||||
export const CURRENCY_DECIMAL_PLACES: Record<string, number> = {
|
||||
'SAR': 2, // Saudi Riyal
|
||||
'USD': 2, // US Dollar
|
||||
'AED': 2, // UAE Dirham
|
||||
'KWD': 3, // Kuwaiti Dinar
|
||||
'OMR': 3, // Omani Rial
|
||||
'BHD': 3, // Bahraini Dinar
|
||||
'JOD': 3, // Jordanian Dinar
|
||||
'PKR': 2, // Pakistani Rupee
|
||||
'JPY': 0, // Japanese Yen (if used)
|
||||
'KRW': 0, // South Korean Won (if used)
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert numeric currency code to ISO currency code
|
||||
* @param numericCode - Numeric currency code (e.g., '682')
|
||||
* @returns ISO currency code (e.g., 'SAR') or the original code if not found
|
||||
*/
|
||||
export function numericToCurrencyCode(numericCode: string | null | undefined): string {
|
||||
if (!numericCode) {
|
||||
return 'SAR'; // Default fallback
|
||||
}
|
||||
|
||||
// If already an ISO code (3 letters), return as is
|
||||
if (/^[A-Z]{3}$/.test(numericCode)) {
|
||||
return numericCode;
|
||||
}
|
||||
|
||||
// Map numeric code to ISO code
|
||||
return NUMERIC_TO_CURRENCY_CODE[numericCode] || numericCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format amount based on currency decimal places
|
||||
* @param amount - Amount to format (number or string)
|
||||
* @param currency - ISO currency code (e.g., 'SAR', 'KWD')
|
||||
* @returns Formatted amount string
|
||||
*/
|
||||
export function formatCurrencyAmount(amount: number | string, currency: string): string {
|
||||
const numAmount = typeof amount === 'string' ? parseFloat(amount) : amount;
|
||||
|
||||
if (isNaN(numAmount)) {
|
||||
return '0';
|
||||
}
|
||||
|
||||
const decimalPlaces = CURRENCY_DECIMAL_PLACES[currency] ?? 2;
|
||||
return numAmount.toFixed(decimalPlaces);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get currency from account or transaction, with fallback
|
||||
* @param accountCurrency - Currency from account entity (may be numeric like '682')
|
||||
* @param transactionCurrency - Currency from transaction entity (may be numeric)
|
||||
* @param fallback - Fallback currency (default: 'SAR')
|
||||
* @returns ISO currency code
|
||||
*/
|
||||
export function getCurrency(
|
||||
accountCurrency?: string | null,
|
||||
transactionCurrency?: string | null,
|
||||
fallback: string = 'SAR'
|
||||
): string {
|
||||
// Convert account currency first (it may be numeric like '682')
|
||||
if (accountCurrency) {
|
||||
const converted = numericToCurrencyCode(accountCurrency);
|
||||
if (converted && converted !== accountCurrency) {
|
||||
return converted; // Successfully converted from numeric to ISO
|
||||
}
|
||||
// If already ISO format, return as is
|
||||
if (/^[A-Z]{3}$/.test(accountCurrency)) {
|
||||
return accountCurrency;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert transaction currency (may be numeric)
|
||||
if (transactionCurrency) {
|
||||
return numericToCurrencyCode(transactionCurrency);
|
||||
}
|
||||
|
||||
return fallback;
|
||||
}
|
||||
@ -1,8 +1,14 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { AllowanceModule } from '~/allowance/allowance.module';
|
||||
import { CronRun } from './entities';
|
||||
import { CronRunRepository } from './repositories';
|
||||
import { BaseCronService } from './services';
|
||||
import { CronRunService } from './services/cron-run.service';
|
||||
import { AllowanceScheduleCron } from './tasks';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
providers: [BaseCronService],
|
||||
imports: [AllowanceModule, TypeOrmModule.forFeature([CronRun])],
|
||||
providers: [BaseCronService, CronRunService, CronRunRepository, AllowanceScheduleCron],
|
||||
})
|
||||
export class CronModule {}
|
||||
|
||||
39
src/cron/entities/cron-run.entity.ts
Normal file
39
src/cron/entities/cron-run.entity.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import {
|
||||
BaseEntity,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
import { CronRunStatus } from '../enums/cron-run-status.enum';
|
||||
|
||||
@Entity('cron_runs')
|
||||
export class CronRun extends BaseEntity {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id!: string;
|
||||
|
||||
@Column({ type: 'varchar', name: 'job_name' })
|
||||
jobName!: string;
|
||||
|
||||
@Column({ type: 'varchar', name: 'status', default: CronRunStatus.SUCCESS })
|
||||
status!: CronRunStatus;
|
||||
|
||||
@Column({ type: 'int', name: 'processed_count', default: 0 })
|
||||
processedCount!: number;
|
||||
|
||||
@Column({ type: 'text', name: 'error_message', nullable: true })
|
||||
errorMessage!: string | null;
|
||||
|
||||
@Column({ type: 'timestamp with time zone', name: 'started_at' })
|
||||
startedAt!: Date;
|
||||
|
||||
@Column({ type: 'timestamp with time zone', name: 'finished_at', nullable: true })
|
||||
finishedAt!: Date | null;
|
||||
|
||||
@CreateDateColumn({ type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP', name: 'created_at' })
|
||||
createdAt!: Date;
|
||||
|
||||
@UpdateDateColumn({ type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP', name: 'updated_at' })
|
||||
updatedAt!: Date;
|
||||
}
|
||||
1
src/cron/entities/index.ts
Normal file
1
src/cron/entities/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './cron-run.entity';
|
||||
4
src/cron/enums/cron-run-status.enum.ts
Normal file
4
src/cron/enums/cron-run-status.enum.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export enum CronRunStatus {
|
||||
SUCCESS = 'SUCCESS',
|
||||
FAILED = 'FAILED',
|
||||
}
|
||||
44
src/cron/repositories/cron-run.repository.ts
Normal file
44
src/cron/repositories/cron-run.repository.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { CronRun } from '../entities';
|
||||
import { CronRunStatus } from '../enums/cron-run-status.enum';
|
||||
|
||||
@Injectable()
|
||||
export class CronRunRepository {
|
||||
constructor(@InjectRepository(CronRun) private readonly cronRunRepository: Repository<CronRun>) {}
|
||||
|
||||
createRun(jobName: string, startedAt: Date): Promise<CronRun> {
|
||||
return this.cronRunRepository.save(
|
||||
this.cronRunRepository.create({
|
||||
jobName,
|
||||
startedAt,
|
||||
status: CronRunStatus.SUCCESS,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
markSuccess(id: string, processedCount: number, finishedAt: Date) {
|
||||
return this.cronRunRepository.update(
|
||||
{ id },
|
||||
{
|
||||
status: CronRunStatus.SUCCESS,
|
||||
processedCount,
|
||||
finishedAt,
|
||||
errorMessage: null,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
markFailure(id: string, processedCount: number, finishedAt: Date, errorMessage: string) {
|
||||
return this.cronRunRepository.update(
|
||||
{ id },
|
||||
{
|
||||
status: CronRunStatus.FAILED,
|
||||
processedCount,
|
||||
finishedAt,
|
||||
errorMessage,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
1
src/cron/repositories/index.ts
Normal file
1
src/cron/repositories/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './cron-run.repository';
|
||||
20
src/cron/services/cron-run.service.ts
Normal file
20
src/cron/services/cron-run.service.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { CronRun } from '../entities';
|
||||
import { CronRunRepository } from '../repositories';
|
||||
|
||||
@Injectable()
|
||||
export class CronRunService {
|
||||
constructor(private readonly cronRunRepository: CronRunRepository) {}
|
||||
|
||||
start(jobName: string): Promise<CronRun> {
|
||||
return this.cronRunRepository.createRun(jobName, new Date());
|
||||
}
|
||||
|
||||
success(id: string, processedCount: number) {
|
||||
return this.cronRunRepository.markSuccess(id, processedCount, new Date());
|
||||
}
|
||||
|
||||
failure(id: string, processedCount: number, errorMessage: string) {
|
||||
return this.cronRunRepository.markFailure(id, processedCount, new Date(), errorMessage);
|
||||
}
|
||||
}
|
||||
@ -1 +1,2 @@
|
||||
export * from './base-cron.service';
|
||||
export * from './cron-run.service';
|
||||
76
src/cron/tasks/allowance-schedule.cron.ts
Normal file
76
src/cron/tasks/allowance-schedule.cron.ts
Normal file
@ -0,0 +1,76 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { Cron, CronExpression } from '@nestjs/schedule';
|
||||
import { AllowanceQueueService } from '~/allowance/services';
|
||||
import { AllowanceScheduleRepository } from '~/allowance/repositories';
|
||||
import { BaseCronService } from '../services';
|
||||
import { CronRunService } from '../services/cron-run.service';
|
||||
|
||||
@Injectable()
|
||||
export class AllowanceScheduleCron {
|
||||
private readonly logger = new Logger(AllowanceScheduleCron.name);
|
||||
private readonly lockKey = 'cron:allowance:enqueue';
|
||||
private readonly jobName = 'allowance.schedule.enqueue';
|
||||
|
||||
constructor(
|
||||
private readonly baseCronService: BaseCronService,
|
||||
private readonly allowanceScheduleRepository: AllowanceScheduleRepository,
|
||||
private readonly allowanceQueueService: AllowanceQueueService,
|
||||
private readonly cronRunService: CronRunService,
|
||||
) {}
|
||||
|
||||
@Cron(CronExpression.EVERY_5_MINUTES)
|
||||
async enqueueDueSchedules() {
|
||||
this.logger.log('Starting allowance schedule cron job');
|
||||
|
||||
const hasLock = await this.baseCronService.acquireLock(this.lockKey, 240);
|
||||
if (!hasLock) {
|
||||
this.logger.warn('Could not acquire lock for allowance cron job - another instance may be running');
|
||||
return;
|
||||
}
|
||||
|
||||
this.logger.log('Lock acquired, starting to process due schedules');
|
||||
const cronRun = await this.cronRunService.start(this.jobName);
|
||||
let processedCount = 0;
|
||||
try {
|
||||
const batchSize = 100;
|
||||
let cursor: { nextRunAt: Date; id: string } | undefined;
|
||||
let processedBatches = 0;
|
||||
|
||||
while (processedBatches < 50) {
|
||||
const schedules = await this.allowanceScheduleRepository.findDueSchedulesBatch(batchSize, cursor);
|
||||
this.logger.log(`Found ${schedules.length} due schedules in batch ${processedBatches + 1}`);
|
||||
|
||||
if (!schedules.length) {
|
||||
this.logger.log('No more due schedules to process');
|
||||
break;
|
||||
}
|
||||
|
||||
for (const schedule of schedules) {
|
||||
this.logger.debug(`Enqueueing schedule ${schedule.id} - juniorId: ${schedule.juniorId}, amount: ${schedule.amount}, nextRunAt: ${schedule.nextRunAt}`);
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
schedules.map((schedule) => this.allowanceQueueService.enqueueSchedule(schedule.id, schedule.nextRunAt)),
|
||||
);
|
||||
|
||||
const last = schedules[schedules.length - 1];
|
||||
cursor = { nextRunAt: last.nextRunAt, id: last.id };
|
||||
processedBatches += 1;
|
||||
processedCount += schedules.length;
|
||||
|
||||
if (schedules.length < batchSize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this.logger.log(`Allowance cron job completed - processed ${processedCount} schedules`);
|
||||
await this.cronRunService.success(cronRun.id, processedCount);
|
||||
} catch (error) {
|
||||
const stack = error instanceof Error ? error.stack : undefined;
|
||||
this.logger.error('Failed to enqueue allowance schedules', stack || error);
|
||||
await this.cronRunService.failure(cronRun.id, processedCount, String(stack || error));
|
||||
} finally {
|
||||
await this.baseCronService.releaseLock(this.lockKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
export * from './allowance-schedule.cron';
|
||||
|
||||
@ -1,10 +1,16 @@
|
||||
import { BadRequestException, ConflictException, forwardRef, Inject, Injectable, Logger } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import moment from 'moment';
|
||||
import { Transactional } from 'typeorm-transactional';
|
||||
import { CountryIso } from '~/common/enums';
|
||||
import { NumericToCountryIso } from '~/common/mappers';
|
||||
import { KycWebhookRequest } from '~/common/modules/neoleap/dtos/requests';
|
||||
import { NeoLeapService } from '~/common/modules/neoleap/services';
|
||||
import { NOTIFICATION_EVENTS } from '~/common/modules/notification/constants/event-names.constant';
|
||||
import {
|
||||
IKycApprovedEvent,
|
||||
IKycRejectedEvent,
|
||||
} from '~/common/modules/notification/interfaces/notification-events.interface';
|
||||
import { GuardianService } from '~/guardian/services';
|
||||
import { CreateJuniorRequestDto } from '~/junior/dtos/request';
|
||||
import { User } from '~/user/entities';
|
||||
@ -23,6 +29,7 @@ export class CustomerService {
|
||||
private readonly guardianService: GuardianService,
|
||||
@Inject(forwardRef(() => NeoLeapService)) private readonly neoleapService: NeoLeapService,
|
||||
private readonly metadataService: MetadataService,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
) {}
|
||||
|
||||
async updateCustomer(userId: string, data: Partial<Customer>): Promise<Customer> {
|
||||
@ -151,6 +158,7 @@ export class CustomerService {
|
||||
}
|
||||
|
||||
const customer = await this.findCustomerById(transaction.customerId);
|
||||
const previousStatus = customer.kycStatus;
|
||||
|
||||
// Update transaction record
|
||||
await this.kycTransactionRepo.updateByStateId(body.stateId, {
|
||||
@ -165,8 +173,32 @@ export class CustomerService {
|
||||
await this.customerRepository.updateCustomer(customer.id, {
|
||||
kycStatus,
|
||||
neoleapExternalCustomerId: body.entity.externalId,
|
||||
rejectionReason: kycStatus === KycStatus.REJECTED ? 'KYC verification failed' : null,
|
||||
});
|
||||
|
||||
// Reload customer with updated data
|
||||
const updatedCustomer = await this.findCustomerById(customer.id);
|
||||
|
||||
// Emit notification event
|
||||
if (kycStatus === KycStatus.APPROVED) {
|
||||
const event: IKycApprovedEvent = {
|
||||
customer: updatedCustomer,
|
||||
previousStatus,
|
||||
timestamp: new Date(),
|
||||
};
|
||||
this.eventEmitter.emit(NOTIFICATION_EVENTS.KYC_APPROVED, event);
|
||||
this.logger.log(`Emitted KYC_APPROVED event for customer ${customer.id}`);
|
||||
} else {
|
||||
const event: IKycRejectedEvent = {
|
||||
customer: updatedCustomer,
|
||||
previousStatus,
|
||||
rejectionReason: updatedCustomer.rejectionReason || 'KYC verification failed',
|
||||
timestamp: new Date(),
|
||||
};
|
||||
this.eventEmitter.emit(NOTIFICATION_EVENTS.KYC_REJECTED, event);
|
||||
this.logger.log(`Emitted KYC_REJECTED event for customer ${customer.id}`);
|
||||
}
|
||||
|
||||
this.logger.log(`KYC updated successfully for customer ${customer.id}, status: ${body.status}, externalId: ${body.entity.externalId}`);
|
||||
}
|
||||
|
||||
|
||||
18
src/db/migrations/1768395622276-AddTimezoneFields.ts
Normal file
18
src/db/migrations/1768395622276-AddTimezoneFields.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
export class AddTimezoneFields1768395622276 implements MigrationInterface {
|
||||
name = 'AddTimezoneFields1768395622276'
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`ALTER TABLE "users" ADD "timezone" character varying(50)`);
|
||||
await queryRunner.query(`ALTER TABLE "devices" ADD "timezone" character varying(50)`);
|
||||
await queryRunner.query(`ALTER TABLE "users" ALTER COLUMN "is_push_enabled" SET DEFAULT true`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`ALTER TABLE "users" ALTER COLUMN "is_push_enabled" SET DEFAULT false`);
|
||||
await queryRunner.query(`ALTER TABLE "devices" DROP COLUMN "timezone"`);
|
||||
await queryRunner.query(`ALTER TABLE "users" DROP COLUMN "timezone"`);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
export class CreateAllowanceSchedulesAndCredits1769423488963 implements MigrationInterface {
|
||||
name = 'CreateAllowanceSchedulesAndCredits1769423488963'
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`CREATE TABLE "allowance_credits" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "schedule_id" uuid NOT NULL, "transaction_id" uuid, "amount" numeric(12,2) NOT NULL, "run_at" TIMESTAMP WITH TIME ZONE NOT NULL, "credited_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), CONSTRAINT "PK_4d8f104f20199b6c23a92e265bf" PRIMARY KEY ("id"))`);
|
||||
await queryRunner.query(`CREATE TABLE "allowance_schedules" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "amount" numeric(12,2) NOT NULL, "frequency" character varying NOT NULL, "status" character varying NOT NULL DEFAULT 'ON', "next_run_at" TIMESTAMP WITH TIME ZONE NOT NULL, "last_run_at" TIMESTAMP WITH TIME ZONE, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "guardian_id" uuid NOT NULL, "junior_id" uuid NOT NULL, CONSTRAINT "PK_27ebe08d13044e8739af557b7a5" PRIMARY KEY ("id"))`);
|
||||
await queryRunner.query(`ALTER TABLE "allowance_credits" ADD CONSTRAINT "FK_9b15567a82f05604001fde8a914" FOREIGN KEY ("schedule_id") REFERENCES "allowance_schedules"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
|
||||
await queryRunner.query(`ALTER TABLE "allowance_credits" ADD CONSTRAINT "FK_b179c206f7bb6f10b5168e583b8" FOREIGN KEY ("transaction_id") REFERENCES "transactions"("id") ON DELETE SET NULL ON UPDATE NO ACTION`);
|
||||
await queryRunner.query(`ALTER TABLE "allowance_schedules" ADD CONSTRAINT "FK_43eb94744e09d8349811c148351" FOREIGN KEY ("guardian_id") REFERENCES "guardians"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
|
||||
await queryRunner.query(`ALTER TABLE "allowance_schedules" ADD CONSTRAINT "FK_dd8a608d7f50cb120fa6f7b163f" FOREIGN KEY ("junior_id") REFERENCES "juniors"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`ALTER TABLE "allowance_schedules" DROP CONSTRAINT "FK_dd8a608d7f50cb120fa6f7b163f"`);
|
||||
await queryRunner.query(`ALTER TABLE "allowance_schedules" DROP CONSTRAINT "FK_43eb94744e09d8349811c148351"`);
|
||||
await queryRunner.query(`ALTER TABLE "allowance_credits" DROP CONSTRAINT "FK_b179c206f7bb6f10b5168e583b8"`);
|
||||
await queryRunner.query(`ALTER TABLE "allowance_credits" DROP CONSTRAINT "FK_9b15567a82f05604001fde8a914"`);
|
||||
await queryRunner.query(`DROP TABLE "allowance_schedules"`);
|
||||
await queryRunner.query(`DROP TABLE "allowance_credits"`);
|
||||
}
|
||||
|
||||
}
|
||||
14
src/db/migrations/1769602683670-CreateCronRunsTable.ts
Normal file
14
src/db/migrations/1769602683670-CreateCronRunsTable.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
export class CreateCronRunsTable1769602683670 implements MigrationInterface {
|
||||
name = 'CreateCronRunsTable1769602683670'
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`CREATE TABLE "cron_runs" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "job_name" character varying NOT NULL, "status" character varying NOT NULL DEFAULT 'SUCCESS', "processed_count" integer NOT NULL DEFAULT '0', "error_message" text, "started_at" TIMESTAMP WITH TIME ZONE NOT NULL, "finished_at" TIMESTAMP WITH TIME ZONE, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), CONSTRAINT "PK_43e18ce1778dc9b20a137b84fd5" PRIMARY KEY ("id"))`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`DROP TABLE "cron_runs"`);
|
||||
}
|
||||
|
||||
}
|
||||
@ -11,3 +11,5 @@ export * from './1765804942393-AddKycFieldsAndTransactions';
|
||||
export * from './1765877128065-AddNationalIdToKycTransactions';
|
||||
export * from './1765891028260-RemoveOldCustomerColumns';
|
||||
export * from './1765975126402-RemoveAddressColumns';
|
||||
export * from './1768395622276-AddTimezoneFields';export * from './1769423488963-CreateAllowanceSchedulesAndCredits';
|
||||
export * from './1769602683670-CreateCronRunsTable';
|
||||
|
||||
@ -11,6 +11,7 @@ import {
|
||||
} from 'typeorm';
|
||||
import { Customer } from '~/customer/entities';
|
||||
import { Junior } from '~/junior/entities';
|
||||
import { AllowanceSchedule } from '~/allowance/entities/allowance-schedule.entity';
|
||||
import { MoneyRequest } from '~/money-request/entities/money-request.entity';
|
||||
|
||||
@Entity('guardians')
|
||||
@ -31,6 +32,9 @@ export class Guardian extends BaseEntity {
|
||||
@OneToMany(() => MoneyRequest, (moneyRequest) => moneyRequest.guardian)
|
||||
moneyRequests!: MoneyRequest[];
|
||||
|
||||
@OneToMany(() => AllowanceSchedule, (schedule) => schedule.guardian)
|
||||
allowanceSchedules!: AllowanceSchedule[];
|
||||
|
||||
@CreateDateColumn({ name: 'created_at', type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP' })
|
||||
createdAt!: Date;
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
import { HealthCheck, HealthCheckService, HttpHealthIndicator, TypeOrmHealthIndicator } from '@nestjs/terminus';
|
||||
import { SkipThrottle } from '@nestjs/throttler';
|
||||
@ -11,6 +12,7 @@ export class HealthController {
|
||||
private readonly healthCheckService: HealthCheckService,
|
||||
private readonly databaseHealthIndicator: TypeOrmHealthIndicator,
|
||||
private readonly httpHealthIndicator: HttpHealthIndicator,
|
||||
private readonly configService: ConfigService,
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
@ -24,9 +26,23 @@ export class HealthController {
|
||||
checkHealthDetails() {
|
||||
const healthIndicators = [
|
||||
() => this.databaseHealthIndicator.pingCheck('database'),
|
||||
// add your own health indicators here
|
||||
];
|
||||
|
||||
const rabbitManagementUrl = this.configService.get<string>('RABBITMQ_MANAGEMENT_URL');
|
||||
if (rabbitManagementUrl) {
|
||||
const user = this.configService.get<string>('RABBITMQ_MANAGEMENT_USER');
|
||||
const pass = this.configService.get<string>('RABBITMQ_MANAGEMENT_PASS');
|
||||
const headers: Record<string, string> = {};
|
||||
if (user && pass) {
|
||||
const encoded = Buffer.from(`${user}:${pass}`).toString('base64');
|
||||
headers.Authorization = `Basic ${encoded}`;
|
||||
}
|
||||
|
||||
healthIndicators.push(() =>
|
||||
this.httpHealthIndicator.pingCheck('rabbitmq', rabbitManagementUrl, { headers }),
|
||||
);
|
||||
}
|
||||
|
||||
return this.healthCheckService.check(healthIndicators);
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,6 +41,7 @@
|
||||
"ALLOWANCE": {
|
||||
"START_DATE_BEFORE_TODAY": "لا يمكن أن يكون تاريخ البدء قبل اليوم.",
|
||||
"START_DATE_AFTER_END_DATE": "لا يمكن أن يكون تاريخ البدء بعد تاريخ النهاية.",
|
||||
"ALREADY_EXISTS": "يوجد جدول مصروف قائم لهذا الطفل بالفعل.",
|
||||
"NOT_FOUND": "لم يتم العثور على المصروف.",
|
||||
"DOES_NOT_BELONG_TO_JUNIOR": "المصروف لا يخص الطفل."
|
||||
},
|
||||
@ -112,14 +113,46 @@
|
||||
"NOT_FOUND": "لم يتم العثور على البطاقة."
|
||||
},
|
||||
"NOTIFICATION": {
|
||||
"CHILD_TOP_UP_TITLE": "تم شحن البطاقة",
|
||||
"CHILD_TOP_UP_MESSAGE": "لقد استلمت {amount} {currency}. الرصيد الإجمالي: {balance} {currency}",
|
||||
"CHILD_SPENDING_TITLE": "تمت العملية بنجاح",
|
||||
"CHILD_SPENDING_MESSAGE": "لقد أنفقت {amount} {currency} في {merchant}. الرصيد: {balance} {currency}",
|
||||
"PARENT_TOP_UP_TITLE": "تأكيد الشحن",
|
||||
"PARENT_TOP_UP_MESSAGE": "لقد قمت بتحويل {amount} {currency} إلى {childName}. الرصيد: {balance} {currency}",
|
||||
"PARENT_SPENDING_TITLE": "تنبيه إنفاق الطفل",
|
||||
"PARENT_SPENDING_MESSAGE": "أنفق {childName} {amount} {currency} في {merchant}. الرصيد: {balance} {currency}",
|
||||
"YOUR_CHILD": "طفلك"
|
||||
"CHILD_TOP_UP_TITLE": "تم إضافة رصيد",
|
||||
"CHILD_TOP_UP_MESSAGE": "تمت إضافة {amount} {currency} إلى بطاقتك. إجمالي الرصيد: {balance} {currency}",
|
||||
"CHILD_INTERNAL_TRANSFER_TITLE": "تم إضافة رصيد",
|
||||
"CHILD_INTERNAL_TRANSFER_MESSAGE": "تمت إضافة {amount} {currency} إلى بطاقتك. إجمالي الرصيد: {balance} {currency}",
|
||||
"PARENT_INTERNAL_TRANSFER_TITLE": "اكتمل التحويل",
|
||||
"PARENT_INTERNAL_TRANSFER_MESSAGE": "تم تحويل {amount} {currency} إلى بطاقة {childName}. رصيد {childName}: {balance} {currency}",
|
||||
"CHILD_SPENDING_TITLE": "عملية شراء ناجحة",
|
||||
"CHILD_SPENDING_MESSAGE": "قمت بإنفاق {amount} {currency} في {merchant}",
|
||||
"PARENT_SPENDING_TITLE": "تنبيه صرف",
|
||||
"PARENT_SPENDING_MESSAGE": "قام {childName} بإنفاق {amount} {currency} في {merchant}. الرصيد المتبقي: {balance} {currency}",
|
||||
"YOUR_CHILD": "طفلك",
|
||||
"MONEY_REQUEST_CREATED_TITLE": "طلب مبلغ مالي",
|
||||
"MONEY_REQUEST_CREATED_MESSAGE": "طلب {childName} مبلغ {amount} {currency} لـ {reason}",
|
||||
"MONEY_REQUEST_APPROVED_TITLE": "تمت الموافقة على طلب المال",
|
||||
"MONEY_REQUEST_APPROVED_MESSAGE": "تمت الموافقة على طلبك بمبلغ {amount} {currency}. تمت إضافة المال إلى حسابك.",
|
||||
"MONEY_REQUEST_DECLINED_TITLE": "تم رفض طلب المال",
|
||||
"MONEY_REQUEST_DECLINED_MESSAGE": "تم رفض طلبك بمبلغ {amount} {currency}. السبب: {reason}",
|
||||
"KYC_APPROVED_TITLE": "تمت الموافقة على التحقق من الهوية",
|
||||
"KYC_APPROVED_MESSAGE": "تمت الموافقة على التحقق من هويتك. يمكنك الآن استخدام جميع ميزات التطبيق.",
|
||||
"KYC_REJECTED_TITLE": "تم رفض التحقق من الهوية",
|
||||
"KYC_REJECTED_MESSAGE": "تم رفض التحقق من هويتك. السبب: {reason}. يرجى مراجعة معلوماتك والمحاولة مرة أخرى.",
|
||||
"CARD_CREATED_TITLE": "تم إنشاء البطاقة",
|
||||
"CARD_CREATED_MESSAGE": "تم إنشاء بطاقتك التي تنتهي بـ {lastFourDigits} بنجاح. يمكنك البدء في استخدامها بمجرد تفعيلها.",
|
||||
"CARD_BLOCKED_TITLE": "تم حظر البطاقة",
|
||||
"CARD_BLOCKED_MESSAGE": "تم حظر بطاقتك التي تنتهي بـ {lastFourDigits}. السبب: {reason}. يرجى الاتصال بالدعم للحصول على المساعدة.",
|
||||
"PROFILE_UPDATED_TITLE": "تم تحديث الملف الشخصي",
|
||||
"PROFILE_UPDATED_MESSAGE": "تم تحديث ملفك الشخصي. التغييرات: {fields}",
|
||||
"PROFILE_EMAIL_UPDATED_TITLE": "تم تحديث البريد الإلكتروني",
|
||||
"PROFILE_EMAIL_UPDATED_MESSAGE": "تم تحديث بريدك الإلكتروني إلى {email}. يرجى التحقق من عنوان بريدك الإلكتروني الجديد.",
|
||||
"PROFILE_PASSWORD_UPDATED_TITLE": "تم تحديث كلمة المرور",
|
||||
"PROFILE_PASSWORD_UPDATED_MESSAGE": "تم تحديث كلمة المرور بنجاح. إذا لم تقم بهذا التغيير، يرجى الاتصال بالدعم فوراً.",
|
||||
"PROFILE_PICTURE_UPDATED_TITLE": "تم تحديث صورة الملف الشخصي",
|
||||
"PROFILE_PICTURE_UPDATED_MESSAGE": "تم تحديث صورة ملفك الشخصي بنجاح.",
|
||||
"PROFILE_NAME_UPDATED_TITLE": "تم تحديث الاسم",
|
||||
"PROFILE_NAME_UPDATED_MESSAGE": "تم تحديث اسمك بنجاح.",
|
||||
"MAINTENANCE_ALERT_TITLE": "صيانة مجدولة",
|
||||
"MAINTENANCE_ALERT_MESSAGE": "{message}",
|
||||
"TRANSACTION_FAILED_TITLE": "فشلت المعاملة",
|
||||
"TRANSACTION_FAILED_MESSAGE": "لم يتم إكمال معاملتك. السبب: {reason}. يرجى المحاولة مرة أخرى أو الاتصال بالدعم إذا استمرت المشكلة.",
|
||||
"SUSPICIOUS_LOGIN_TITLE": "تم اكتشاف تسجيل دخول مشبوه",
|
||||
"SUSPICIOUS_LOGIN_MESSAGE": "اكتشفنا محاولة تسجيل دخول من {location} ({ipAddress}) باستخدام {device}. إذا لم تكن أنت، يرجى تغيير كلمة المرور فوراً والاتصال بالدعم."
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,10 +94,12 @@
|
||||
"name": "الاسم",
|
||||
"amount": "المبلغ",
|
||||
"type": "النوع",
|
||||
"frequency": "التكرار",
|
||||
"startDate": "تاريخ البدء",
|
||||
"endDate": "تاريخ النهاية",
|
||||
"numberOfTransactions": "عدد المعاملات",
|
||||
"juniorId": "معرّف الطفل"
|
||||
"juniorId": "معرّف الطفل",
|
||||
"status": "الحالة"
|
||||
},
|
||||
"allowanceChangeRequest": {
|
||||
"reason": "السبب",
|
||||
|
||||
@ -40,6 +40,7 @@
|
||||
"ALLOWANCE": {
|
||||
"START_DATE_BEFORE_TODAY": "The start date cannot be before today.",
|
||||
"START_DATE_AFTER_END_DATE": "The start date cannot be after the end date.",
|
||||
"ALREADY_EXISTS": "An allowance schedule already exists for this child.",
|
||||
"NOT_FOUND": "The allowance was not found.",
|
||||
"DOES_NOT_BELONG_TO_JUNIOR": "The allowance does not belong to the junior."
|
||||
},
|
||||
@ -111,14 +112,46 @@
|
||||
"NOT_FOUND": "The card was not found."
|
||||
},
|
||||
"NOTIFICATION": {
|
||||
"CHILD_TOP_UP_TITLE": "Card Topped Up",
|
||||
"CHILD_TOP_UP_MESSAGE": "You received {amount} {currency}. Total balance: {balance} {currency}",
|
||||
"CHILD_TOP_UP_TITLE": "Funds Credited",
|
||||
"CHILD_TOP_UP_MESSAGE": "{amount} {currency} has been added to your card. Total balance: {balance} {currency}",
|
||||
"CHILD_INTERNAL_TRANSFER_TITLE": "Funds Credited",
|
||||
"CHILD_INTERNAL_TRANSFER_MESSAGE": "{amount} {currency} has been added to your card. Total balance: {balance} {currency}",
|
||||
"PARENT_INTERNAL_TRANSFER_TITLE": "Internal Transfer Completed",
|
||||
"PARENT_INTERNAL_TRANSFER_MESSAGE": "{amount} {currency} has been transferred to {childName}'s card. {childName}'s balance is {balance} {currency}",
|
||||
"CHILD_SPENDING_TITLE": "Purchase Successful",
|
||||
"CHILD_SPENDING_MESSAGE": "You spent {amount} {currency} at {merchant}. Balance: {balance} {currency}",
|
||||
"PARENT_TOP_UP_TITLE": "Top-Up Confirmation",
|
||||
"PARENT_TOP_UP_MESSAGE": "You transferred {amount} {currency} to {childName}. Balance: {balance} {currency}",
|
||||
"PARENT_SPENDING_TITLE": "Child Spending Alert",
|
||||
"PARENT_SPENDING_MESSAGE": "{childName} spent {amount} {currency} at {merchant}. Balance: {balance} {currency}",
|
||||
"YOUR_CHILD": "Your child"
|
||||
"CHILD_SPENDING_MESSAGE": "You spent {amount} {currency} at {merchant}",
|
||||
"PARENT_SPENDING_TITLE": "Spending Alert",
|
||||
"PARENT_SPENDING_MESSAGE": "{childName} spent {amount} {currency} at {merchant}. Remaining balance: {balance} {currency}",
|
||||
"YOUR_CHILD": "Your child",
|
||||
"MONEY_REQUEST_CREATED_TITLE": "Money Request",
|
||||
"MONEY_REQUEST_CREATED_MESSAGE": "{childName} has requested {amount} {currency} for {reason}.",
|
||||
"MONEY_REQUEST_APPROVED_TITLE": "Money Request Approved",
|
||||
"MONEY_REQUEST_APPROVED_MESSAGE": "Your request for {amount} {currency} has been approved. The money has been added to your account.",
|
||||
"MONEY_REQUEST_DECLINED_TITLE": "Money Request Declined",
|
||||
"MONEY_REQUEST_DECLINED_MESSAGE": "Your request for {amount} {currency} has been declined. Reason: {reason}",
|
||||
"KYC_APPROVED_TITLE": "KYC Verification Approved",
|
||||
"KYC_APPROVED_MESSAGE": "Your KYC verification has been approved. You can now use all features of the app.",
|
||||
"KYC_REJECTED_TITLE": "KYC Verification Rejected",
|
||||
"KYC_REJECTED_MESSAGE": "Your KYC verification has been rejected. Reason: {reason}. Please review your information and try again.",
|
||||
"CARD_CREATED_TITLE": "Card Created",
|
||||
"CARD_CREATED_MESSAGE": "Your card ending in {lastFourDigits} has been created successfully. You can start using it once it's activated.",
|
||||
"CARD_BLOCKED_TITLE": "Card Blocked",
|
||||
"CARD_BLOCKED_MESSAGE": "Your card ending in {lastFourDigits} has been blocked. Reason: {reason}. Please contact support for assistance.",
|
||||
"PROFILE_UPDATED_TITLE": "Profile Updated",
|
||||
"PROFILE_UPDATED_MESSAGE": "Your profile has been updated. Changes: {fields}",
|
||||
"PROFILE_EMAIL_UPDATED_TITLE": "Email Updated",
|
||||
"PROFILE_EMAIL_UPDATED_MESSAGE": "Your email has been updated to {email}. Please verify your new email address.",
|
||||
"PROFILE_PASSWORD_UPDATED_TITLE": "Password Updated",
|
||||
"PROFILE_PASSWORD_UPDATED_MESSAGE": "Your password has been successfully updated. If you did not make this change, please contact support immediately.",
|
||||
"PROFILE_PICTURE_UPDATED_TITLE": "Profile Picture Updated",
|
||||
"PROFILE_PICTURE_UPDATED_MESSAGE": "Your profile picture has been updated successfully.",
|
||||
"PROFILE_NAME_UPDATED_TITLE": "Name Updated",
|
||||
"PROFILE_NAME_UPDATED_MESSAGE": "Your name has been updated successfully.",
|
||||
"MAINTENANCE_ALERT_TITLE": "Scheduled Maintenance",
|
||||
"MAINTENANCE_ALERT_MESSAGE": "{message}",
|
||||
"TRANSACTION_FAILED_TITLE": "Transaction Failed",
|
||||
"TRANSACTION_FAILED_MESSAGE": "Your transaction could not be completed. Reason: {reason}. Please try again or contact support if the issue persists.",
|
||||
"SUSPICIOUS_LOGIN_TITLE": "Suspicious Login Detected",
|
||||
"SUSPICIOUS_LOGIN_MESSAGE": "We detected a login attempt from {location} ({ipAddress}) using {device}. If this was not you, please change your password immediately and contact support."
|
||||
}
|
||||
}
|
||||
|
||||
@ -98,10 +98,12 @@
|
||||
"name": "Name",
|
||||
"amount": "Amount",
|
||||
"type": "Type",
|
||||
"frequency": "Frequency",
|
||||
"startDate": "Start date",
|
||||
"endDate": "End date",
|
||||
"numberOfTransactions": "Number of transactions",
|
||||
"juniorId": "Junior ID"
|
||||
"juniorId": "Junior ID",
|
||||
"status": "Status"
|
||||
},
|
||||
"allowanceChangeRequest": {
|
||||
"reason": "Reason",
|
||||
|
||||
@ -11,6 +11,7 @@ import {
|
||||
PrimaryColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
import { AllowanceSchedule } from '~/allowance/entities/allowance-schedule.entity';
|
||||
import { Customer } from '~/customer/entities';
|
||||
import { Guardian } from '~/guardian/entities/guradian.entity';
|
||||
import { MoneyRequest } from '~/money-request/entities/money-request.entity';
|
||||
@ -45,6 +46,9 @@ export class Junior extends BaseEntity {
|
||||
@OneToMany(() => MoneyRequest, (moneyRequest) => moneyRequest.junior)
|
||||
moneyRequests!: MoneyRequest[];
|
||||
|
||||
@OneToMany(() => AllowanceSchedule, (schedule) => schedule.junior)
|
||||
allowanceSchedules!: AllowanceSchedule[];
|
||||
|
||||
@CreateDateColumn({ name: 'created_at', type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP' })
|
||||
createdAt!: Date;
|
||||
|
||||
|
||||
@ -79,6 +79,8 @@ export class MoneyRequestsRepository {
|
||||
'junior.customer',
|
||||
'junior.customer.user',
|
||||
'junior.customer.user.profilePicture',
|
||||
'junior.customer.cards',
|
||||
'junior.customer.cards.account',
|
||||
'guardian',
|
||||
'guardian.customer',
|
||||
'guardian.customer.user',
|
||||
|
||||
1
src/types/amqplib.d.ts
vendored
Normal file
1
src/types/amqplib.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
declare module 'amqplib';
|
||||
@ -1,15 +1,15 @@
|
||||
import { Body, Controller, Get, Headers, HttpCode, HttpStatus, Patch, UseGuards } from '@nestjs/common';
|
||||
import { Body, Controller, Get, HttpCode, HttpStatus, Patch, UseGuards } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { VerifyOtpRequestDto } from '~/auth/dtos/request';
|
||||
import { UserResponseDto } from '~/auth/dtos/response';
|
||||
import { IJwtPayload } from '~/auth/interfaces';
|
||||
import { DEVICE_ID_HEADER } from '~/common/constants';
|
||||
import { AuthenticatedUser } from '~/common/decorators';
|
||||
import { AccessTokenGuard } from '~/common/guards';
|
||||
import { ApiDataResponse } from '~/core/decorators';
|
||||
import { ResponseFactory } from '~/core/utils';
|
||||
import { UpdateNotificationsSettingsRequestDto, UpdateUserRequestDto } from '../dtos/request';
|
||||
import { UpdateEmailRequestDto } from '../dtos/request/update-email.request.dto';
|
||||
import { NotificationsSettingsResponseDto } from '../dtos/response/notifications-settings.response.dto';
|
||||
import { UserService } from '../services';
|
||||
|
||||
@Controller('profile')
|
||||
@ -45,13 +45,22 @@ export class UserController {
|
||||
return this.userService.verifyEmail(user.sub, otp);
|
||||
}
|
||||
|
||||
@Get('notifications-settings')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiDataResponse(NotificationsSettingsResponseDto)
|
||||
async getNotificationSettings(@AuthenticatedUser() { sub }: IJwtPayload) {
|
||||
const user = await this.userService.findUserOrThrow({ id: sub });
|
||||
return ResponseFactory.data(new NotificationsSettingsResponseDto({
|
||||
isPushEnabled: user.isPushEnabled ?? true,
|
||||
}));
|
||||
}
|
||||
|
||||
@Patch('notifications-settings')
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
async updateNotificationSettings(
|
||||
@AuthenticatedUser() user: IJwtPayload,
|
||||
@Body() data: UpdateNotificationsSettingsRequestDto,
|
||||
@Headers(DEVICE_ID_HEADER) deviceId: string,
|
||||
) {
|
||||
return this.userService.updateNotificationSettings(user.sub, data, deviceId);
|
||||
return this.userService.updateNotificationSettings(user.sub, data, data.deviceId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,23 +2,19 @@ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsBoolean, IsOptional, IsString, ValidateIf } from 'class-validator';
|
||||
import { i18nValidationMessage as i18n } from 'nestjs-i18n';
|
||||
export class UpdateNotificationsSettingsRequestDto {
|
||||
@ApiProperty()
|
||||
@IsBoolean({ message: i18n('validation.IsBoolean', { path: 'general', property: 'customer.isEmailEnabled' }) })
|
||||
@IsOptional()
|
||||
isEmailEnabled!: boolean;
|
||||
|
||||
@ApiProperty()
|
||||
@ApiPropertyOptional({ example: true, description: 'Enable/disable push notifications (default: true)', default: true })
|
||||
@IsBoolean({ message: i18n('validation.IsBoolean', { path: 'general', property: 'customer.isPushEnabled' }) })
|
||||
@IsOptional()
|
||||
isPushEnabled!: boolean;
|
||||
isPushEnabled?: boolean;
|
||||
|
||||
@ApiProperty()
|
||||
@IsBoolean({ message: i18n('validation.IsBoolean', { path: 'general', property: 'customer.isSmsEnabled' }) })
|
||||
@IsOptional()
|
||||
isSmsEnabled!: boolean;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@ApiPropertyOptional({ example: 'cXYzABC:APA91bH...', description: 'Firebase Cloud Messaging token (required if enabling push)' })
|
||||
@IsString({ message: i18n('validation.IsString', { path: 'general', property: 'auth.fcmToken' }) })
|
||||
@ValidateIf((o) => o.isPushEnabled)
|
||||
@ValidateIf((o) => o.isPushEnabled !== false)
|
||||
@IsOptional()
|
||||
fcmToken?: string;
|
||||
|
||||
@ApiPropertyOptional({ example: 'device-123', description: 'Device identifier (optional, will be found automatically if not provided)' })
|
||||
@IsOptional()
|
||||
@IsString({ message: i18n('validation.IsString', { path: 'general', property: 'auth.deviceId' }) })
|
||||
deviceId?: string;
|
||||
}
|
||||
|
||||
@ -34,4 +34,12 @@ export class UpdateUserRequestDto {
|
||||
@IsDateString({}, { message: i18n('validation.IsDateString', { path: 'general', property: 'customer.dateOfBirth' }) })
|
||||
@IsOptional()
|
||||
dateOfBirth!: Date;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
example: 'Asia/Riyadh',
|
||||
description: 'User preferred timezone for reports/statements (e.g., "Asia/Riyadh", "America/New_York"). Leave empty or "Auto" to use device timezone.',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString({ message: i18n('validation.IsString', { path: 'general', property: 'user.timezone' }) })
|
||||
timezone?: string;
|
||||
}
|
||||
|
||||
@ -0,0 +1,10 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class NotificationsSettingsResponseDto {
|
||||
@ApiProperty({ example: true, description: 'Push notifications enabled/disabled' })
|
||||
isPushEnabled!: boolean;
|
||||
|
||||
constructor(data: { isPushEnabled: boolean }) {
|
||||
this.isPushEnabled = data.isPushEnabled;
|
||||
}
|
||||
}
|
||||
@ -18,6 +18,9 @@ export class Device {
|
||||
@Column('varchar', { name: 'fcm_token', nullable: true })
|
||||
fcmToken?: string | null;
|
||||
|
||||
@Column('varchar', { name: 'timezone', nullable: true, length: 50 })
|
||||
timezone?: string | null; // e.g., "Asia/Riyadh", "America/New_York" - auto-detected from device
|
||||
|
||||
@Column('timestamp with time zone', { name: 'last_access_on', default: () => 'CURRENT_TIMESTAMP' })
|
||||
lastAccessOn!: Date;
|
||||
|
||||
|
||||
@ -61,12 +61,15 @@ export class User extends BaseEntity {
|
||||
@Column({ name: 'is_email_enabled', default: false })
|
||||
isEmailEnabled!: boolean;
|
||||
|
||||
@Column({ name: 'is_push_enabled', default: false })
|
||||
@Column({ name: 'is_push_enabled', default: true })
|
||||
isPushEnabled!: boolean;
|
||||
|
||||
@Column({ name: 'is_sms_enabled', default: false })
|
||||
isSmsEnabled!: boolean;
|
||||
|
||||
@Column('varchar', { name: 'timezone', nullable: true, length: 50 })
|
||||
timezone?: string | null; // User's preferred timezone for reports/statements (e.g., "Asia/Riyadh")
|
||||
|
||||
@Column('text', { nullable: true, array: true, name: 'roles' })
|
||||
roles!: Roles[];
|
||||
|
||||
|
||||
@ -26,4 +26,8 @@ export class DeviceRepository {
|
||||
getTokens(userId: string) {
|
||||
return this.deviceRepository.find({ where: { userId, fcmToken: Not(IsNull()) }, select: ['fcmToken'] });
|
||||
}
|
||||
|
||||
findUserDevices(userId: string) {
|
||||
return this.deviceRepository.find({ where: { userId } });
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,4 +31,9 @@ export class DeviceService {
|
||||
|
||||
return devices.map((device) => device.fcmToken!);
|
||||
}
|
||||
|
||||
findUserDevices(userId: string) {
|
||||
this.logger.log(`Finding all devices for user ${userId}`);
|
||||
return this.deviceRepository.findUserDevices(userId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +1,12 @@
|
||||
import { BadRequestException, forwardRef, Inject, Injectable, Logger } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { FindOptionsWhere } from 'typeorm';
|
||||
import { Transactional } from 'typeorm-transactional';
|
||||
import { CountryIso } from '~/common/enums';
|
||||
import { NOTIFICATION_EVENTS } from '~/common/modules/notification/constants/event-names.constant';
|
||||
import { IProfileUpdatedEvent } from '~/common/modules/notification/interfaces/notification-events.interface';
|
||||
import { NotificationsService } from '~/common/modules/notification/services';
|
||||
import { OtpScope, OtpType } from '~/common/modules/otp/enums';
|
||||
import { OtpService } from '~/common/modules/otp/services';
|
||||
@ -28,6 +31,7 @@ export class UserService {
|
||||
private readonly documentService: DocumentService,
|
||||
private readonly otpService: OtpService,
|
||||
private readonly ociService: OciService,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
) {}
|
||||
|
||||
async findUser(where: FindOptionsWhere<User> | FindOptionsWhere<User>[], includeSignedUrl = false) {
|
||||
@ -131,22 +135,55 @@ export class UserService {
|
||||
|
||||
async updateNotificationSettings(userId: string, data: UpdateNotificationsSettingsRequestDto, deviceId?: string) {
|
||||
this.logger.log(`Updating notification settings for user ${userId} with data ${JSON.stringify(data)}`);
|
||||
if (data.isPushEnabled && !data.fcmToken) {
|
||||
|
||||
const isPushEnabled = data.isPushEnabled ?? true;
|
||||
|
||||
if (isPushEnabled && !data.fcmToken) {
|
||||
throw new BadRequestException('USER.FCM_TOKEN_REQUIRED');
|
||||
}
|
||||
|
||||
if (data.isPushEnabled && !deviceId) {
|
||||
throw new BadRequestException('DEVICE_ID_REQUIRED');
|
||||
}
|
||||
if (isPushEnabled && data.fcmToken) {
|
||||
let targetDeviceId = deviceId;
|
||||
|
||||
if (data.isPushEnabled && deviceId && data.fcmToken) {
|
||||
await this.deviceService.updateDevice(deviceId, { fcmToken: data.fcmToken, userId });
|
||||
if (!targetDeviceId) {
|
||||
const userDevices = await this.deviceService.findUserDevices(userId);
|
||||
if (userDevices.length > 0) {
|
||||
targetDeviceId = userDevices[0].deviceId;
|
||||
this.logger.log(`No deviceId provided, using first device: ${targetDeviceId}`);
|
||||
} else {
|
||||
targetDeviceId = `device-${userId}-${Date.now()}`;
|
||||
this.logger.log(`No device found, creating new device: ${targetDeviceId}`);
|
||||
await this.deviceService.createDevice({
|
||||
deviceId: targetDeviceId,
|
||||
userId,
|
||||
fcmToken: data.fcmToken,
|
||||
lastAccessOn: new Date(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (targetDeviceId) {
|
||||
const existingDevice = await this.deviceService.findUserDeviceById(targetDeviceId, userId);
|
||||
if (existingDevice) {
|
||||
await this.deviceService.updateDevice(targetDeviceId, { fcmToken: data.fcmToken, userId });
|
||||
} else {
|
||||
const anyDevice = await this.deviceService.findByDeviceId(targetDeviceId);
|
||||
if (anyDevice) {
|
||||
await this.deviceService.updateDevice(targetDeviceId, { fcmToken: data.fcmToken, userId });
|
||||
} else {
|
||||
await this.deviceService.createDevice({
|
||||
deviceId: targetDeviceId,
|
||||
userId,
|
||||
fcmToken: data.fcmToken,
|
||||
lastAccessOn: new Date(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await this.userRepository.update(userId, {
|
||||
isPushEnabled: data.isPushEnabled,
|
||||
isEmailEnabled: data.isEmailEnabled,
|
||||
isSmsEnabled: data.isSmsEnabled,
|
||||
isPushEnabled: isPushEnabled,
|
||||
});
|
||||
}
|
||||
|
||||
@ -216,6 +253,19 @@ export class UserService {
|
||||
}
|
||||
await this.customerService.updateCustomer(userId, customerData);
|
||||
}
|
||||
|
||||
const updatedUser = await this.findUserOrThrow({ id: userId });
|
||||
const updatedFields = Object.keys(data).filter(key => (data as any)[key] !== undefined);
|
||||
|
||||
if (updatedFields.length > 0) {
|
||||
const event: IProfileUpdatedEvent = {
|
||||
user: updatedUser,
|
||||
updatedFields,
|
||||
timestamp: new Date(),
|
||||
};
|
||||
this.eventEmitter.emit(NOTIFICATION_EVENTS.PROFILE_UPDATED, event);
|
||||
this.logger.log(`Emitted PROFILE_UPDATED event for user ${userId}, updated fields: ${updatedFields.join(', ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
async updateUserEmail(userId: string, email: string) {
|
||||
@ -246,6 +296,15 @@ export class UserService {
|
||||
throw new BadRequestException('USER.NOT_FOUND');
|
||||
}
|
||||
|
||||
const updatedUser = await this.findUserOrThrow({ id: userId });
|
||||
const event: IProfileUpdatedEvent = {
|
||||
user: updatedUser,
|
||||
updatedFields: ['email'],
|
||||
timestamp: new Date(),
|
||||
};
|
||||
this.eventEmitter.emit(NOTIFICATION_EVENTS.PROFILE_UPDATED, event);
|
||||
this.logger.log(`Emitted PROFILE_UPDATED event for user ${userId}, updated field: email`);
|
||||
|
||||
return this.otpService.generateAndSendOtp({
|
||||
userId,
|
||||
recipient: email,
|
||||
|
||||
Reference in New Issue
Block a user