feat(nestjs): migrate to NestJS

This commit is contained in:
Ahmed Bouhuolia
2025-04-07 11:51:24 +02:00
parent f068218a16
commit 55fcc908ef
3779 changed files with 631 additions and 195332 deletions

View File

@@ -0,0 +1,82 @@
import {
Controller,
Get,
Post,
Delete,
Param,
Body,
Req,
Res,
Next,
HttpStatus,
} from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { ApiTags } from '@nestjs/swagger';
import { PaymentServicesApplication } from './PaymentServicesApplication';
import { EditPaymentMethodDTO } from './types';
@ApiTags('PaymentServices')
@Controller('payment-services')
export class PaymentServicesController {
constructor(
private readonly paymentServicesApp: PaymentServicesApplication,
) {}
@Get('/')
async getPaymentServicesSpecificInvoice(@Res() res: Response) {
const paymentServices =
await this.paymentServicesApp.getPaymentServicesForInvoice();
return res.status(HttpStatus.OK).send({ paymentServices });
}
@Get('/state')
async getPaymentMethodsState(@Res() res: Response) {
const paymentMethodsState =
await this.paymentServicesApp.getPaymentMethodsState();
return res.status(HttpStatus.OK).send({ data: paymentMethodsState });
}
@Get('/:paymentServiceId')
async getPaymentService(
@Param('paymentServiceId') paymentServiceId: number,
@Req() req: Request,
@Res() res: Response,
@Next() next: NextFunction,
) {
const paymentService =
await this.paymentServicesApp.getPaymentService(paymentServiceId);
return res.status(HttpStatus.OK).send({ data: paymentService });
}
@Post('/:paymentMethodId')
async updatePaymentMethod(
@Param('paymentMethodId') paymentMethodId: number,
@Body() updatePaymentMethodDTO: EditPaymentMethodDTO,
@Res() res: Response,
) {
await this.paymentServicesApp.editPaymentMethod(
paymentMethodId,
updatePaymentMethodDTO,
);
return res.status(HttpStatus.OK).send({
id: paymentMethodId,
message: 'The given payment method has been updated.',
});
}
@Delete('/:paymentMethodId')
async deletePaymentMethod(
@Param('paymentMethodId') paymentMethodId: number,
@Res() res: Response,
) {
await this.paymentServicesApp.deletePaymentMethod(paymentMethodId);
return res.status(HttpStatus.NO_CONTENT).send({
id: paymentMethodId,
message: 'The payment method has been deleted.',
});
}
}

View File

@@ -0,0 +1,32 @@
import { Module } from '@nestjs/common';
import { DeletePaymentMethodService } from './commands/DeletePaymentMethodService';
import { EditPaymentMethodService } from './commands/EditPaymentMethodService';
import { GetPaymentMethodService } from './queries/GetPaymentService';
import { GetPaymentServicesSpecificInvoice } from './queries/GetPaymentServicesSpecificInvoice';
import { GetPaymentMethodsStateService } from './queries/GetPaymentMethodsState';
import { PaymentServicesApplication } from './PaymentServicesApplication';
import { PaymentServicesController } from './PaymentServices.controller';
import { RegisterTenancyModel } from '../Tenancy/TenancyModels/Tenancy.module';
import { PaymentIntegration } from './models/PaymentIntegration.model';
import { TransactionPaymentServiceEntry } from './models/TransactionPaymentServiceEntry.model';
import { StripePaymentModule } from '../StripePayment/StripePayment.module';
const models = [
RegisterTenancyModel(PaymentIntegration),
RegisterTenancyModel(TransactionPaymentServiceEntry),
];
@Module({
imports: [...models, StripePaymentModule],
exports: [...models],
providers: [
DeletePaymentMethodService,
EditPaymentMethodService,
GetPaymentMethodService,
GetPaymentMethodsStateService,
GetPaymentServicesSpecificInvoice,
PaymentServicesApplication,
],
controllers: [PaymentServicesController],
})
export class PaymentServicesModule {}

View File

@@ -0,0 +1,73 @@
import { GetPaymentServicesSpecificInvoice } from './queries/GetPaymentServicesSpecificInvoice';
import { DeletePaymentMethodService } from './commands/DeletePaymentMethodService';
import { EditPaymentMethodService } from './commands/EditPaymentMethodService';
import { EditPaymentMethodDTO, GetPaymentMethodsPOJO } from './types';
import { GetPaymentMethodsStateService } from './queries/GetPaymentMethodsState';
import { GetPaymentMethodService } from './queries/GetPaymentService';
import { Injectable } from '@nestjs/common';
@Injectable()
export class PaymentServicesApplication {
constructor(
private readonly getPaymentServicesSpecificInvoice: GetPaymentServicesSpecificInvoice,
private readonly deletePaymentMethodService: DeletePaymentMethodService,
private readonly editPaymentMethodService: EditPaymentMethodService,
private readonly getPaymentMethodsStateService: GetPaymentMethodsStateService,
private readonly getPaymentMethodService: GetPaymentMethodService,
) {}
/**
* Retrieves the payment services for a specific invoice.
* @param {number} invoiceId - The ID of the invoice.
* @returns {Promise<any>} The payment services for the specified invoice.
*/
public async getPaymentServicesForInvoice(): Promise<any> {
return this.getPaymentServicesSpecificInvoice.getPaymentServicesInvoice();
}
/**
* Retrieves specific payment service details.
* @param {number} paymentServiceId - Payment service id.
*/
public async getPaymentService(paymentServiceId: number) {
return this.getPaymentMethodService.getPaymentMethod(paymentServiceId);
}
/**
* Deletes the given payment method.
* @param {number} paymentIntegrationId - Payment integration id.
* @returns {Promise<void>}
*/
public async deletePaymentMethod(
paymentIntegrationId: number,
): Promise<void> {
return this.deletePaymentMethodService.deletePaymentMethod(
paymentIntegrationId,
);
}
/**
* Edits the given payment method.
* @param {number} paymentIntegrationId - Payment integration id.
* @param {EditPaymentMethodDTO} editPaymentMethodDTO - Edit payment method DTO.
* @returns {Promise<void>}
*/
public async editPaymentMethod(
paymentIntegrationId: number,
editPaymentMethodDTO: EditPaymentMethodDTO,
): Promise<void> {
return this.editPaymentMethodService.editPaymentMethod(
paymentIntegrationId,
editPaymentMethodDTO,
);
}
/**
* Retrieves the payment state providing state.
* @param {number} tenantId
* @returns {Promise<GetPaymentMethodsPOJO>}
*/
public async getPaymentMethodsState(): Promise<GetPaymentMethodsPOJO> {
return this.getPaymentMethodsStateService.getPaymentMethodsState();
}
}

View File

@@ -0,0 +1,59 @@
import { Knex } from 'knex';
import { Inject, Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
import { PaymentIntegration } from '../models/PaymentIntegration.model';
import { TransactionPaymentServiceEntry } from '../models/TransactionPaymentServiceEntry.model';
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
import { events } from '@/common/events/events';
@Injectable()
export class DeletePaymentMethodService {
constructor(
private readonly uow: UnitOfWork,
private readonly eventEmitter: EventEmitter2,
@Inject(PaymentIntegration.name)
private readonly paymentIntegrationModel: TenantModelProxy<
typeof PaymentIntegration
>,
@Inject(TransactionPaymentServiceEntry.name)
private readonly transactionPaymentServiceEntryModel: TenantModelProxy<
typeof TransactionPaymentServiceEntry
>,
) {}
/**
* Deletes the given payment integration.
* @param {number} paymentIntegrationId
* @returns {Promise<void>}
*/
public async deletePaymentMethod(
paymentIntegrationId: number,
): Promise<void> {
const paymentIntegration = await this.paymentIntegrationModel()
.query()
.findById(paymentIntegrationId)
.throwIfNotFound();
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
// Delete payment methods links.
await this.transactionPaymentServiceEntryModel()
.query(trx)
.where('paymentIntegrationId', paymentIntegrationId)
.delete();
// Delete the payment integration.
await this.paymentIntegrationModel()
.query(trx)
.findById(paymentIntegrationId)
.delete();
// Triggers `onPaymentMethodDeleted` event.
await this.eventEmitter.emitAsync(events.paymentMethod.onDeleted, {
paymentIntegrationId,
});
});
}
}

View File

@@ -0,0 +1,58 @@
import { Knex } from 'knex';
import { EditPaymentMethodDTO } from '../types';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
import { PaymentIntegration } from '../models/PaymentIntegration.model';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { Inject, Injectable } from '@nestjs/common';
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
import { events } from '@/common/events/events';
@Injectable()
export class EditPaymentMethodService {
constructor(
private readonly uow: UnitOfWork,
private readonly eventEmitter: EventEmitter2,
@Inject(PaymentIntegration.name)
private readonly paymentIntegrationModel: TenantModelProxy<
typeof PaymentIntegration
>,
) {}
/**
* Edits the given payment method.
* @param {number} paymentIntegrationId - The ID of the payment method.
* @param {EditPaymentMethodDTO} editPaymentMethodDTO
* @returns {Promise<void>}
*/
async editPaymentMethod(
paymentIntegrationId: number,
editPaymentMethodDTO: EditPaymentMethodDTO,
): Promise<void> {
const paymentMethod = await this.paymentIntegrationModel()
.query()
.findById(paymentIntegrationId)
.throwIfNotFound();
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
// Triggers `onPaymentMethodEditing` event.
await this.eventEmitter.emitAsync(events.paymentMethod.onEditing, {
paymentIntegrationId,
editPaymentMethodDTO,
trx,
});
await this.paymentIntegrationModel()
.query(trx)
.findById(paymentIntegrationId)
.patch({
...editPaymentMethodDTO,
});
// Triggers `onPaymentMethodEdited` event.
await this.eventEmitter.emitAsync(events.paymentMethod.onEdited, {
paymentIntegrationId,
editPaymentMethodDTO,
trx,
});
});
}
}

View File

@@ -0,0 +1,67 @@
import { BaseModel } from '@/models/Model';
export class PaymentIntegration extends BaseModel {
readonly name!: string;
readonly service!: string;
readonly paymentEnabled!: boolean;
readonly payoutEnabled!: boolean;
readonly accountId!: string;
readonly options!: Record<string, any>;
static get tableName() {
return 'payment_integrations';
}
static get idColumn() {
return 'id';
}
static get virtualAttributes() {
return ['fullEnabled'];
}
static get jsonAttributes() {
return ['options'];
}
get fullEnabled() {
return this.paymentEnabled && this.payoutEnabled;
}
/**
*
*/
static get modifiers() {
return {
/**
* Query to filter enabled payment and payout.
*/
fullEnabled(query) {
query.where('paymentEnabled', true).andWhere('payoutEnabled', true);
},
};
}
static get jsonSchema() {
return {
type: 'object',
required: ['name', 'service'],
properties: {
id: { type: 'integer' },
service: { type: 'string' },
paymentEnabled: { type: 'boolean' },
payoutEnabled: { type: 'boolean' },
accountId: { type: 'string' },
options: {
type: 'object',
properties: {
bankAccountId: { type: 'number' },
clearingAccountId: { type: 'number' },
},
},
createdAt: { type: 'string', format: 'date-time' },
updatedAt: { type: 'string', format: 'date-time' },
},
};
}
}

View File

@@ -0,0 +1,56 @@
import { Model } from 'objection';
import { BaseModel } from '@/models/Model';
import { PaymentIntegration } from './PaymentIntegration.model';
export class TransactionPaymentServiceEntry extends BaseModel {
readonly referenceId!: number;
readonly referenceType!: string;
readonly paymentIntegrationId!: number;
readonly enable!: boolean;
readonly options!: Record<string, any>;
readonly paymentIntegration: PaymentIntegration;
/**
* Table name
*/
static get tableName() {
return 'transactions_payment_methods';
}
/**
* Json schema of the model.
*/
static get jsonSchema() {
return {
type: 'object',
required: ['paymentIntegrationId'],
properties: {
id: { type: 'integer' },
referenceId: { type: 'integer' },
referenceType: { type: 'string' },
paymentIntegrationId: { type: 'integer' },
enable: { type: 'boolean' },
options: { type: 'object' },
},
};
}
/**
* Relationship mapping.
*/
static get relationMappings() {
const { PaymentIntegration } = require('./PaymentIntegration.model');
return {
paymentIntegration: {
relation: Model.BelongsToOneRelation,
modelClass: PaymentIntegration,
join: {
from: 'transactions_payment_methods.paymentIntegrationId',
to: 'payment_integrations.id',
},
},
};
}
}

View File

@@ -0,0 +1,77 @@
import { Inject, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { GetPaymentMethodsPOJO } from '../types';
import { GetStripeAuthorizationLinkService } from '../../StripePayment/GetStripeAuthorizationLink';
import { PaymentIntegration } from '../models/PaymentIntegration.model';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable()
export class GetPaymentMethodsStateService {
constructor(
private readonly getStripeAuthorizationLinkService: GetStripeAuthorizationLinkService,
private readonly configService: ConfigService,
@Inject(PaymentIntegration.name)
private readonly paymentIntegrationModel: TenantModelProxy<
typeof PaymentIntegration
>,
) {}
/**
* Retrieves the payment state provising state.
* @param {number} tenantId
* @returns {Promise<GetPaymentMethodsPOJO>}
*/
public async getPaymentMethodsState(): Promise<GetPaymentMethodsPOJO> {
const stripePayment = await this.paymentIntegrationModel()
.query()
.orderBy('createdAt', 'ASC')
.findOne({
service: 'Stripe',
});
const isStripeAccountCreated = !!stripePayment;
const isStripePaymentEnabled = stripePayment?.paymentEnabled;
const isStripePayoutEnabled = stripePayment?.payoutEnabled;
const isStripeEnabled = stripePayment?.fullEnabled;
const stripePaymentMethodId = stripePayment?.id || null;
const stripeAccountId = stripePayment?.accountId || null;
const stripePublishableKey = this.configService.get(
'stripePayment.publishableKey',
);
const stripeCurrencies = ['USD', 'EUR'];
const stripeRedirectUrl = 'https://your-stripe-redirect-url.com';
const isStripeServerConfigured = this.isStripePaymentConfigured();
const stripeAuthLink =
this.getStripeAuthorizationLinkService.getStripeAuthLink();
const paymentMethodPOJO: GetPaymentMethodsPOJO = {
stripe: {
isStripeAccountCreated,
isStripePaymentEnabled,
isStripePayoutEnabled,
isStripeEnabled,
isStripeServerConfigured,
stripeAccountId,
stripePaymentMethodId,
stripePublishableKey,
stripeCurrencies,
stripeAuthLink,
stripeRedirectUrl,
},
};
return paymentMethodPOJO;
}
/**
* Determines if Stripe payment is configured.
* @returns {boolean}
*/
private isStripePaymentConfigured() {
return (
this.configService.get('stripePayment.secretKey') &&
this.configService.get('stripePayment.publishableKey') &&
this.configService.get('stripePayment.webhooksSecret')
);
}
}

View File

@@ -0,0 +1,30 @@
import { Inject, Injectable } from '@nestjs/common';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
import { GetPaymentMethodsPOJO } from '../types';
import { PaymentIntegration } from '../models/PaymentIntegration.model';
import { ModelObject } from 'objection';
@Injectable()
export class GetPaymentMethodService {
constructor(
@Inject(PaymentIntegration.name)
private readonly paymentIntegrationModel: TenantModelProxy<
typeof PaymentIntegration
>,
) {}
/**
* Retrieves the payment state provising state.
* @returns {Promise<GetPaymentMethodsPOJO>}
*/
public async getPaymentMethod(
paymentServiceId: number,
): Promise<ModelObject<PaymentIntegration>> {
const stripePayment = await this.paymentIntegrationModel()
.query()
.findById(paymentServiceId)
.throwIfNotFound();
return stripePayment;
}
}

View File

@@ -0,0 +1,34 @@
import { Inject, Injectable } from '@nestjs/common';
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
import { GetPaymentServicesSpecificInvoiceTransformer } from './GetPaymentServicesSpecificInvoiceTransformer';
import { PaymentIntegration } from '../models/PaymentIntegration.model';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable()
export class GetPaymentServicesSpecificInvoice {
constructor(
private readonly transform: TransformerInjectable,
@Inject(PaymentIntegration.name)
private readonly paymentIntegrationModel: TenantModelProxy<
typeof PaymentIntegration
>,
) {}
/**
* Retrieves the payment services of the given invoice.
* @param {number} invoiceId
* @returns
*/
async getPaymentServicesInvoice() {
const paymentGateways = await this.paymentIntegrationModel()
.query()
.modify('fullEnabled')
.orderBy('name', 'ASC');
return this.transform.transform(
paymentGateways,
new GetPaymentServicesSpecificInvoiceTransformer(),
);
}
}

View File

@@ -0,0 +1,19 @@
import { Transformer } from '@/modules/Transformer/Transformer';
export class GetPaymentServicesSpecificInvoiceTransformer extends Transformer {
/**
* Exclude attributes.
* @returns {string[]}
*/
public excludeAttributes = (): string[] => {
return ['accountId'];
};
public includeAttributes = (): string[] => {
return ['serviceFormatted'];
};
public serviceFormatted(method) {
return 'Stripe';
}
}

View File

@@ -0,0 +1,81 @@
import { ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
IsBoolean,
IsNumber,
IsOptional,
IsString,
ValidateNested,
} from 'class-validator';
class EditPaymentMethodOptionsDto {
@IsOptional()
@IsNumber()
bankAccountId?: number;
@IsOptional()
@IsNumber()
clearningAccountId?: number;
@IsOptional()
@IsBoolean()
showVisa?: boolean;
@IsOptional()
@IsBoolean()
showMasterCard?: boolean;
@IsOptional()
@IsBoolean()
showDiscover?: boolean;
@IsOptional()
@IsBoolean()
showAmer?: boolean;
@IsOptional()
@IsBoolean()
showJcb?: boolean;
@IsOptional()
@IsBoolean()
showDiners?: boolean;
}
export class EditPaymentMethodDTO {
@IsOptional()
@ValidateNested()
@Type(() => EditPaymentMethodOptionsDto)
@ApiPropertyOptional({
type: () => EditPaymentMethodOptionsDto,
description: 'Edit payment method options',
})
options?: EditPaymentMethodOptionsDto;
@IsOptional()
@IsString()
@ApiPropertyOptional({
type: String,
description: 'Payment method name',
})
name?: string;
}
export interface GetPaymentMethodsPOJO {
stripe: {
isStripeAccountCreated: boolean;
isStripePaymentEnabled: boolean;
isStripePayoutEnabled: boolean;
isStripeEnabled: boolean;
isStripeServerConfigured: boolean;
stripeAccountId: string | null;
stripePaymentMethodId: number | null;
stripePublishableKey: string | null;
stripeAuthLink: string;
stripeCurrencies: Array<string>;
stripeRedirectUrl: string | null;
};
}