Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { SearchModule } from './search/search.module';
import { BackupModule } from './backup/backup.module';
import { TrackingModule } from './tracking/tracking.module';
import { NotificationsModule } from './notifications/notifications.module';
import { TransactionsModule } from './transactions/transactions.module';
import { EmailDigestModule } from './email-digest/email-digest.module';
@Module({
imports: [
Expand Down Expand Up @@ -61,6 +62,7 @@ import { EmailDigestModule } from './email-digest/email-digest.module';
BackupModule,
TrackingModule,
NotificationsModule,
TransactionsModule,
EmailDigestModule,
],
controllers: [AppController],
Expand Down
6 changes: 3 additions & 3 deletions src/email/email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ export class EmailService {
options.emailType,
trackingId,
);

const baseUrl = this.configService.get<string>('API_URL', 'http://localhost:3000/api');
const pixelUrl = `${baseUrl}/track/open/${trackingId}.png`;

options.context = {
...options.context,
trackingPixel: pixelUrl,
Expand All @@ -163,7 +163,7 @@ export class EmailService {
removeOnComplete: true,
removeOnFail: false,
});

this.logger.log(`📧 Email to ${options.to} queued for subject: ${options.subject}`);
} catch (error) {
this.logger.error(`❌ Failed to queue email to ${options.to}: ${error.message}`);
Expand Down
2 changes: 1 addition & 1 deletion src/notifications/sms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class SmsService {
// Mock implementation for SMS delivery
// In a production environment, this would integrate with a provider like Twilio, Vonage, or AWS SNS.
this.logger.log(`📱 Sending SMS to ${to}: ${message}`);

// Simulate successful delivery
return true;
}
Expand Down
60 changes: 60 additions & 0 deletions src/transactions/dto/transaction-search.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Type } from 'class-transformer';
import {
IsDateString,
IsEnum,
IsInt,
IsNumber,
IsOptional,
IsString,
IsUUID,
Max,
Min,
} from 'class-validator';
import { TransactionStatus } from '../../types/prisma.types';

export class TransactionSearchQueryDto {
@IsOptional()
@IsDateString()
dateFrom?: string;

@IsOptional()
@IsDateString()
dateTo?: string;

@IsOptional()
@Type(() => Number)
@IsNumber()
@Min(0)
minAmount?: number;

@IsOptional()
@Type(() => Number)
@IsNumber()
@Min(0)
maxAmount?: number;

@IsOptional()
@IsEnum(TransactionStatus)
status?: TransactionStatus;

@IsOptional()
@IsUUID('4')
propertyId?: string;

@IsOptional()
@IsString()
property?: string;

@IsOptional()
@Type(() => Number)
@IsInt()
@Min(1)
page: number = 1;

@IsOptional()
@Type(() => Number)
@IsInt()
@Min(1)
@Max(100)
limit: number = 20;
}
13 changes: 12 additions & 1 deletion src/transactions/transactions.controller.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import { Body, Controller, Get, Param, Patch, Post, UseGuards } from '@nestjs/common';
import { Body, Controller, Get, Param, Patch, Post, Query, UseGuards } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { CurrentUser } from '../auth/decorators/current-user.decorator';
import { AuthUserPayload } from '../auth/types/auth-user.type';
import { TransactionSearchQueryDto } from './dto/transaction-search.dto';
import {
CreateTransactionDto,
CreateTransactionTaxStrategyDto,
UpdateTransactionTaxStrategyDto,
} from './dto/transaction.dto';
import { TransactionsService } from './transactions.service';

@ApiTags('transactions')
@Controller('transactions')
export class TransactionsController {
constructor(private readonly transactionsService: TransactionsService) {}

@UseGuards(JwtAuthGuard)
@Get('search')
@ApiOperation({ summary: 'Search transactions with filters and pagination' })
@ApiResponse({ status: 200, description: 'Transaction search results returned successfully' })
search(@Query() query: TransactionSearchQueryDto, @CurrentUser() user: AuthUserPayload) {
return this.transactionsService.search(query, user);
}

@UseGuards(JwtAuthGuard)
@Post()
create(@Body() createTransactionDto: CreateTransactionDto, @CurrentUser() user: AuthUserPayload) {
Expand Down
7 changes: 2 additions & 5 deletions src/transactions/transactions.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import { TransactionsController } from './transactions.controller';
import { TransactionsService } from './transactions.service';
import { DisputesService } from './disputes.service';
import { TimelineService } from './timeline.service';
import { TransactionsController } from './transactions.controller';
import { DisputesController } from './disputes.controller';
import { TimelineController } from './timeline.controller';
import { PrismaModule } from '../database/prisma.module';
import { NotificationsModule } from '../notifications/notifications.module';

@Module({
imports: [PrismaModule, NotificationsModule],
controllers: [TransactionsController],
providers: [TransactionsService],
controllers: [TransactionsController, DisputesController, TimelineController],
providers: [TransactionsService, DisputesService, TimelineService],
exports: [TransactionsService],
})
export class TransactionsModule {}
116 changes: 116 additions & 0 deletions src/transactions/transactions.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from '../database/prisma.service';
import { TransactionsService } from './transactions.service';
import { TransactionStatus, UserRole } from '../types/prisma.types';

describe('TransactionsService', () => {
let service: TransactionsService;
let prisma: {
transaction: {
findMany: jest.Mock;
count: jest.Mock;
};
};

beforeEach(async () => {
prisma = {
transaction: {
findMany: jest.fn().mockResolvedValue([{ id: 'tx-1' }]),
count: jest.fn().mockResolvedValue(1),
},
};

const module: TestingModule = await Test.createTestingModule({
providers: [
TransactionsService,
{
provide: PrismaService,
useValue: prisma,
},
],
}).compile();

service = module.get(TransactionsService);
});

it('searches user transactions with status, date, amount, property filters, and pagination', async () => {
const result = await service.search(
{
status: TransactionStatus.COMPLETED,
dateFrom: '2026-04-01',
dateTo: '2026-04-29',
minAmount: 100000,
maxAmount: 250000,
property: 'Lagos',
propertyId: '550e8400-e29b-41d4-a716-446655440000',
page: 2,
limit: 10,
},
{
sub: 'user-1',
email: 'buyer@example.com',
role: UserRole.USER as any,
type: 'access',
},
);

expect(prisma.transaction.findMany).toHaveBeenCalledWith(
expect.objectContaining({
skip: 10,
take: 10,
orderBy: { createdAt: 'desc' },
where: {
AND: expect.arrayContaining([
{ OR: [{ buyerId: 'user-1' }, { sellerId: 'user-1' }] },
{ status: TransactionStatus.COMPLETED },
{ propertyId: '550e8400-e29b-41d4-a716-446655440000' },
{
property: {
OR: expect.arrayContaining([
{ title: { contains: 'Lagos', mode: 'insensitive' } },
{ address: { contains: 'Lagos', mode: 'insensitive' } },
]),
},
},
{
createdAt: {
gte: new Date('2026-04-01'),
lte: new Date('2026-04-29T23:59:59.999Z'),
},
},
{
amount: {
gte: expect.any(Object),
lte: expect.any(Object),
},
},
]),
},
}),
);
expect(prisma.transaction.count).toHaveBeenCalledWith({
where: prisma.transaction.findMany.mock.calls[0][0].where,
});
expect(result).toEqual({
total: 1,
page: 2,
limit: 10,
totalPages: 1,
items: [{ id: 'tx-1' }],
});
});

it('lets admins search all transactions without buyer or seller scoping', async () => {
await service.search(
{ page: 1, limit: 20 },
{
sub: 'admin-1',
email: 'admin@example.com',
role: UserRole.ADMIN as any,
type: 'access',
},
);

expect(prisma.transaction.findMany.mock.calls[0][0].where).toEqual({});
});
});
Loading
Loading