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
6 changes: 3 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ model User {
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()
Notification Notification?

@@map("users")
}
Expand Down Expand Up @@ -207,11 +207,11 @@ model VerificationStatus {

model Notification {
id String @id @default(uuid()) @db.Uuid
userId String @unique @map("user_id") @db.Uuid
userId String @map("user_id") @db.Uuid
packageId String? @map("package_id") @db.Uuid
tripId String? @map("trip_id") @db.Uuid
content String @db.Text
isSeen Boolean @default(false)
unread Boolean @default(true)

// Relations
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
Expand Down
3 changes: 3 additions & 0 deletions src/modules/dashboard/dashboard.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export class DashboardResponseDto {

@Expose()
totalWalletBalance: number;

@Expose()
notificationCount: number;

@Expose()
role?: AuthRoles;
Expand Down
20 changes: 17 additions & 3 deletions src/modules/dashboard/dashboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export class DashboardService {
) {}

async getDashboard(userId: string) {
const { transporter, wallet, role, ...user } = await this.prisma.user
const {
transporter,
wallet,
role,
...user
} = await this.prisma.user
.findFirstOrThrow({
where: { id: userId },
select: {
Expand All @@ -42,9 +47,17 @@ export class DashboardService {
},
},
role: true,
_count: {
select: {
notifications: {
where: {
unread: true
}
}
}
}
},
})
.catch((error: Error) => {
}).catch((error: Error) => {
formatPrismaError(error);
throw error;
});
Expand Down Expand Up @@ -84,6 +97,7 @@ export class DashboardService {
experience,
bio: transporter?.bio,
statistics,
notificationCount: user._count.notifications
};
}

Expand Down
25 changes: 19 additions & 6 deletions src/modules/financial/financial.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export class FinancialService {
tx: PrismaTransaction = this.prisma
) {
const skip = (page - 1) * limit;
return tx.wallet.findUniqueOrThrow({

const wallet = await tx.wallet.findUniqueOrThrow({
where: { userId },
include: {
transactions: {
Expand All @@ -35,6 +36,18 @@ export class FinancialService {
formatPrismaError(error);
throw error;
});

return {
...wallet,
balance: wallet.balance.toString(),
totalEarned: wallet.totalEarned.toString(),
totalSpent: wallet.totalSpent.toString(),
transactions: wallet.transactions.map(transaction => ({
...transaction,
amount: transaction.amount.toString(),
balanceBefore: transaction.balanceBefore?.toString(),
}))
};
}

async addFunds(
Expand Down Expand Up @@ -62,7 +75,7 @@ export class FinancialService {
walletId: wallet.id,
transactionType: TransactionTypeEnum.deposit,
amount: BigInt(amount),
balanceBefore: wallet.balance,
balanceBefore: BigInt(wallet.balance),
reason: 'Funds added to wallet.',
gatewayTransactionId
}
Expand Down Expand Up @@ -117,7 +130,7 @@ export class FinancialService {
const finalPrice = matchedRequest.package.finalPrice;

const senderWallet = await this.getWallet(senderId, 1, 0);
if (senderWallet.balance < BigInt(finalPrice)) {
if (Number(senderWallet.balance) < finalPrice) {
throw new BadRequestException(BadRequestMessages.NotEnoughBalance);
}

Expand All @@ -142,7 +155,7 @@ export class FinancialService {
walletId: senderWallet.id,
transactionType: TransactionTypeEnum.escrow,
amount: finalPrice,
balanceBefore: senderWallet.balance,
balanceBefore: BigInt(senderWallet.balance),
reason: `Escrowed payment for package ${matchedRequest.package.id}.`,
matchedRequestId: matchedRequest.id
}
Expand All @@ -153,7 +166,7 @@ export class FinancialService {
walletId: transporterWallet.id,
transactionType: TransactionTypeEnum.escrow,
amount: matchedRequest.request.offeredPrice,
balanceBefore: transporterWallet.balance,
balanceBefore: BigInt(transporterWallet.balance),
reason: `Escrowed payment for package ${matchedRequest.package.id}.`,
matchedRequestId: matchedRequest.id
}
Expand Down Expand Up @@ -263,7 +276,7 @@ export class FinancialService {
walletId: transporterWallet.id,
transactionType: TransactionTypeEnum.release,
amount: BigInt(transporterEarnings),
balanceBefore: transporterWallet.balance,
balanceBefore: BigInt(transporterWallet.balance),
reason: `Payment received for package ${matchedRequest.packageId}.`,
matchedRequestId: matchedRequest.id,
}
Expand Down
28 changes: 21 additions & 7 deletions src/modules/notification/notification.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +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';

@Injectable()
export class NotificationService {
Expand Down Expand Up @@ -35,13 +36,26 @@ export class NotificationService {
limit = 10
) {
const skip = (page - 1) * limit;
return this.prisma.notification.findMany({
where: { userId },
orderBy: {
createdAt: 'desc'
},
skip,
take: limit
return this.prisma.$transaction(async tx => {
// Update notifications => unread: true
await tx.notification.updateMany({
where: { userId },
data: {
unread: false
}
});

return this.prisma.notification.findMany({
where: { userId },
orderBy: {
createdAt: 'desc'
},
skip,
take: limit
});
}).catch((error: Error) => {
formatPrismaError(error);
throw error;
});
}
}
26 changes: 24 additions & 2 deletions src/modules/trip/trip.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,8 @@ export class TripService {
async finishTrip(id: string) {
const {
status: tripStatus,
matchedRequests
matchedRequests,
transporter
} = await this.prisma.trip.findUniqueOrThrow({
where: { id },
select: {
Expand All @@ -860,6 +861,12 @@ export class TripService {
select: {
deliveryTime: true
}
},
transporter: {
select: {
id: true,
firstTripDate: true,
}
}
}
}).catch((error: Error) => {
Expand All @@ -876,7 +883,22 @@ export class TripService {
throw new BadRequestException(BadRequestMessages.CannotFinishTrip);
}

return this.updateStatus(id, TripStatusEnum.completed);
// Update firstTripDate/lastTripDate
const updateTransporter: Prisma.TransporterUpdateInput = {};
if (!transporter.firstTripDate) {
updateTransporter.firstTripDate = new Date();
} else {
updateTransporter.lastTripDate = new Date();
}

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

return this.updateStatus(id, TripStatusEnum.completed, tx);
});
}

async addTripNote(
Expand Down
Loading