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
16 changes: 9 additions & 7 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ model User {
isActive Boolean @default(true) @map("is_active")

// Relations
transporter Transporter?
packages Package[]
addresses Address[]
wallet Wallet?
transporter Transporter?
packages Package[]
addresses Address[]
wallet Wallet?
notifications Notification[]

createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz()
deletedAt DateTime? @map("deleted_at") @db.Timestamptz()
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz()
deletedAt DateTime? @map("deleted_at") @db.Timestamptz()

@@map("users")
}
Expand Down Expand Up @@ -366,6 +366,7 @@ model PackageRecipient {
model Package {
id String @id @default(uuid()) @db.Uuid
senderId String @map("sender_id") @db.Uuid
code Int @default(autoincrement())
items Json? @map("items_description") @db.JsonB
originAddressId String @map("origin_address_id") @db.Uuid
recipientId String @map("recipient_id") @db.Uuid
Expand Down Expand Up @@ -403,6 +404,7 @@ model Package {
model Trip {
id String @id @default(uuid()) @db.Uuid
transporterId String @map("transporter_id") @db.Uuid
code Int @default(autoincrement())
originId String @map("origin_id") @db.Uuid
destinationId String @map("destination_id") @db.Uuid
tripType TripTypeEnum @map("trip_type")
Expand Down
2 changes: 1 addition & 1 deletion src/modules/notification/notification.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { PrismaTransaction } from '../prisma/prisma.types';
import { formatPrismaError } from 'src/common/utilities';
import { formatPrismaError } from '../../common/utilities';

@Injectable()
export class NotificationService {
Expand Down
3 changes: 3 additions & 0 deletions src/modules/package/dto/package-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export class PackageCompactResponseDto {
@Expose()
id: string;

@Expose()
code: number;

@Expose()
items: string[];

Expand Down
3 changes: 3 additions & 0 deletions src/modules/package/dto/tracking-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class PackageTrackingDto {
@Expose()
sender: SenderDto;

@Expose()
code: number;

@Type(() => RecipientDto)
@Expose()
recipient: RecipientDto;
Expand Down
1 change: 1 addition & 0 deletions src/modules/package/package.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ export class PackageService {
},
package: {
select: {
code: true,
sender: {
select: {
firstName: true,
Expand Down
3 changes: 3 additions & 0 deletions src/modules/trip/dto/matched-request-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class MatchedPackageDto {
@Expose()
id: string;

@Expose()
code: number;

@Type(() => UserCompactDto)
@Expose()
sender: UserCompactDto;
Expand Down
5 changes: 4 additions & 1 deletion src/modules/trip/dto/trip-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { TripStatusEnum, TripTypeEnum } from '../../../../generated/prisma';
import { CityDto } from '../../map/dto/city.dto';
import { Expose, Transform, Type } from 'class-transformer';
import { Expose, Type } from 'class-transformer';
import { VehicleCompactResponseDto } from '../../vehicle/dto/vehicle-response.dto';
import { ApiProperty } from '@nestjs/swagger';

export class TripCompactResponseDto {
@Expose()
id: string;

@Expose()
code: number;

@ApiProperty({ type: CityDto })
@Expose()
@Type(() => CityDto)
Expand Down
43 changes: 40 additions & 3 deletions src/modules/trip/trip.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,19 +642,55 @@ describe('TripService', () => {
it('should finish trip successfully', async () => {
prisma.trip.findUniqueOrThrow.mockResolvedValueOnce({
status: TripStatusEnum.in_progress,
matchedRequests: [{ deliveryTime: new Date() }]
matchedRequests: [{ deliveryTime: new Date() }],
transporter: {
id: 'transporter-123',
firstTripDate: null
}
} as any);
prisma.$transaction.mockImplementation(async (callback) => callback(prisma));
prisma.trip.update.mockResolvedValue({ ...mockTrip, status: TripStatusEnum.completed });
prisma.transporter.update.mockResolvedValue({} as any);

const result = await service.finishTrip('trip-123');

expect(result).toEqual({ ...mockTrip, status: TripStatusEnum.completed });
expect(prisma.transporter.update).toHaveBeenCalledWith({
where: { id: 'transporter-123' },
data: { firstTripDate: expect.any(Date) }
});
});

it('should finish trip successfully when transporter has firstTripDate', async () => {
prisma.trip.findUniqueOrThrow.mockResolvedValueOnce({
status: TripStatusEnum.in_progress,
matchedRequests: [{ deliveryTime: new Date() }],
transporter: {
id: 'transporter-123',
firstTripDate: new Date('2023-01-01')
}
} as any);
prisma.$transaction.mockImplementation(async (callback) => callback(prisma));
prisma.trip.update.mockResolvedValue({ ...mockTrip, status: TripStatusEnum.completed });
prisma.transporter.update.mockResolvedValue({} as any);

const result = await service.finishTrip('trip-123');

expect(result).toEqual({ ...mockTrip, status: TripStatusEnum.completed });
expect(prisma.transporter.update).toHaveBeenCalledWith({
where: { id: 'transporter-123' },
data: { lastTripDate: expect.any(Date) }
});
});

it('should throw when not all packages are delivered', async () => {
prisma.trip.findUniqueOrThrow.mockResolvedValueOnce({
status: TripStatusEnum.in_progress,
matchedRequests: [{ deliveryTime: null }]
matchedRequests: [{ deliveryTime: null }],
transporter: {
id: 'transporter-123',
firstTripDate: null
}
} as any);

await expect(service.finishTrip('trip-123')).rejects.toThrow(
Expand Down Expand Up @@ -900,6 +936,7 @@ describe('TripService', () => {
package: {
select: {
id: true,
code: true,
sender: {
select: {
firstName: true,
Expand All @@ -908,12 +945,12 @@ describe('TripService', () => {
phoneNumber: true
}
},
status: true,
items: true,
originAddress: true,
recipient: true,
weight: true,
dimensions: true,
status: true,
packageValue: true,
isFragile: true,
isPerishable: true,
Expand Down
4 changes: 3 additions & 1 deletion src/modules/trip/trip.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ export class TripService {
package: {
select: {
id: true,
code: true,
sender: {
select: {
firstName: true,
Expand Down Expand Up @@ -892,7 +893,8 @@ export class TripService {
}

return this.prisma.$transaction(async tx => {
tx.transporter.update({
// Update transporter
await tx.transporter.update({
where: { id: transporter.id },
data: updateTransporter
});
Expand Down