chore: add notification service for all user action
This commit is contained in:
@@ -10,12 +10,19 @@ import { Discount } from "../discounts/entities/discount.entity";
|
||||
import { UsageDiscount } from "../discounts/entities/usage-discount.entity";
|
||||
import { DiscountRepository } from "../discounts/repositories/discount.repository";
|
||||
import { UsageDiscountRepository } from "../discounts/repositories/usage-discount.repository";
|
||||
import { NotificationModule } from "../notifications/notifications.module";
|
||||
import { UsersModule } from "../users/users.module";
|
||||
import { UtilsModule } from "../utils/utils.module";
|
||||
import { WalletsModule } from "../wallets/wallets.module";
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Invoice, InvoiceItem, Discount, UsageDiscount]), UsersModule, WalletsModule, UtilsModule],
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([Invoice, InvoiceItem, Discount, UsageDiscount]),
|
||||
UsersModule,
|
||||
WalletsModule,
|
||||
UtilsModule,
|
||||
NotificationModule,
|
||||
],
|
||||
providers: [InvoicesService, InvoicesRepository, DiscountRepository, UsageDiscountRepository],
|
||||
controllers: [InvoicesController],
|
||||
exports: [InvoicesService],
|
||||
|
||||
@@ -9,6 +9,7 @@ import { AuthMessage, DiscountMessage, InvoiceMessage, WalletMessage } from "../
|
||||
import { VerifyOtpWithUserId } from "../../auth/DTO/verify-otp.dto";
|
||||
import { DiscountRepository } from "../../discounts/repositories/discount.repository";
|
||||
import { UsageDiscountRepository } from "../../discounts/repositories/usage-discount.repository";
|
||||
import { NotificationsService } from "../../notifications/providers/notifications.service";
|
||||
import { SubscriptionPlan } from "../../subscriptions/entities/subscription.entity";
|
||||
import { UsersService } from "../../users/providers/users.service";
|
||||
import { OTPService } from "../../utils/providers/otp.service";
|
||||
@@ -28,6 +29,7 @@ export class InvoicesService {
|
||||
private readonly logger = new Logger(InvoicesService.name);
|
||||
|
||||
constructor(
|
||||
private readonly notificationsService: NotificationsService,
|
||||
private readonly invoiceRepository: InvoicesRepository,
|
||||
private readonly discountRepository: DiscountRepository,
|
||||
private readonly usageDiscountRepository: UsageDiscountRepository,
|
||||
@@ -41,38 +43,63 @@ export class InvoicesService {
|
||||
///********************************** */
|
||||
|
||||
async createInvoiceAdmin(createDto: CreateInvoiceDto) {
|
||||
//
|
||||
const invoiceItems = createDto.items.map((item) => {
|
||||
return {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
try {
|
||||
const invoiceItems = createDto.items.map((item) => ({
|
||||
name: item.name,
|
||||
count: item.count,
|
||||
unitPrice: item.unitPrice,
|
||||
discount: item.discount || 0,
|
||||
totalPrice: item.unitPrice * item.count - (item.unitPrice * item.count * item.discount) / 100,
|
||||
};
|
||||
});
|
||||
//
|
||||
const totalPrice = invoiceItems.reduce((sum, item) => new Decimal(item.totalPrice).add(sum), new Decimal(0));
|
||||
const tax = totalPrice.mul(0.1);
|
||||
//
|
||||
const dueDate = new Date();
|
||||
dueDate.setDate(dueDate.getDate() + 5);
|
||||
//
|
||||
const invoice = this.invoiceRepository.create({
|
||||
user: { id: createDto.userId },
|
||||
totalPrice: totalPrice.toNumber(),
|
||||
items: invoiceItems,
|
||||
tax: tax.toNumber(),
|
||||
dueDate,
|
||||
});
|
||||
//
|
||||
await this.invoiceRepository.save(invoice);
|
||||
//TODO: notify user
|
||||
}));
|
||||
|
||||
return {
|
||||
message: InvoiceMessage.CREATED,
|
||||
invoice,
|
||||
};
|
||||
const totalPrice = invoiceItems.reduce((sum, item) => new Decimal(item.totalPrice).add(sum), new Decimal(0));
|
||||
const tax = totalPrice.mul(0.1);
|
||||
|
||||
const dueDate = new Date();
|
||||
dueDate.setDate(dueDate.getDate() + 5);
|
||||
|
||||
const invoice = queryRunner.manager.create(Invoice, {
|
||||
user: { id: createDto.userId },
|
||||
totalPrice: totalPrice.toNumber(),
|
||||
items: invoiceItems,
|
||||
tax: tax.toNumber(),
|
||||
dueDate,
|
||||
});
|
||||
|
||||
await queryRunner.manager.save(Invoice, invoice);
|
||||
|
||||
const user = await this.usersService.findOneByIdWithQueryRunner(createDto.userId, queryRunner);
|
||||
|
||||
await this.notificationsService.createInvoiceCreationNotification(
|
||||
createDto.userId,
|
||||
{
|
||||
invoiceId: invoice.numericId.toString(),
|
||||
dueDate: invoice.dueDate,
|
||||
price: new Decimal(invoice.totalPrice).toNumber(),
|
||||
userPhone: user.phone,
|
||||
userEmail: user.email,
|
||||
items: invoiceItems.map((item) => item.name).join(", "),
|
||||
},
|
||||
queryRunner,
|
||||
);
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return {
|
||||
message: InvoiceMessage.CREATED,
|
||||
invoice,
|
||||
};
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
throw error;
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
|
||||
//********************************** */
|
||||
|
||||
Reference in New Issue
Block a user