mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
fix: invoice generate sharable link
This commit is contained in:
@@ -22,6 +22,7 @@ import {
|
||||
CreateSaleInvoiceDto,
|
||||
EditSaleInvoiceDto,
|
||||
} from './dtos/SaleInvoice.dto';
|
||||
import { GenerateShareLink } from './commands/GenerateInvoicePaymentLink.service';
|
||||
|
||||
@Injectable()
|
||||
export class SaleInvoiceApplication {
|
||||
@@ -39,6 +40,7 @@ export class SaleInvoiceApplication {
|
||||
private getSaleInvoiceStateService: GetSaleInvoiceState,
|
||||
private sendSaleInvoiceMailService: SendSaleInvoiceMail,
|
||||
private getSaleInvoiceMailStateService: GetSaleInvoiceMailState,
|
||||
private generateShareLinkService: GenerateShareLink,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -202,4 +204,23 @@ export class SaleInvoiceApplication {
|
||||
saleInvoiceid,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the given sale invoice sharable link.
|
||||
* @param {number} saleInvoiceId
|
||||
* @param {string} publicity
|
||||
* @param {string} expiryTime
|
||||
* @returns
|
||||
*/
|
||||
public generateSaleInvoiceSharableLink(
|
||||
saleInvoiceId: number,
|
||||
publicity: string = 'private',
|
||||
expiryTime: string = '',
|
||||
) {
|
||||
return this.generateShareLinkService.generatePaymentLink(
|
||||
saleInvoiceId,
|
||||
publicity,
|
||||
expiryTime,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,12 +38,14 @@ import { AcceptType } from '@/constants/accept-type';
|
||||
import { SaleInvoiceResponseDto } from './dtos/SaleInvoiceResponse.dto';
|
||||
import { PaginatedResponseDto } from '@/common/dtos/PaginatedResults.dto';
|
||||
import { SaleInvoiceStateResponseDto } from './dtos/SaleInvoiceState.dto';
|
||||
import { GenerateSaleInvoiceSharableLinkResponseDto } from './dtos/generateSaleInvoiceSharableLinkResponse.dto';
|
||||
|
||||
@Controller('sale-invoices')
|
||||
@ApiTags('Sale Invoices')
|
||||
@ApiExtraModels(SaleInvoiceResponseDto)
|
||||
@ApiExtraModels(PaginatedResponseDto)
|
||||
@ApiExtraModels(SaleInvoiceStateResponseDto)
|
||||
@ApiExtraModels(GenerateSaleInvoiceSharableLinkResponseDto)
|
||||
@ApiHeader({
|
||||
name: 'organization-id',
|
||||
description: 'The organization id',
|
||||
@@ -318,4 +320,25 @@ export class SaleInvoicesController {
|
||||
): Promise<SaleInvoiceMailState> {
|
||||
return this.saleInvoiceApplication.getSaleInvoiceMailState(id);
|
||||
}
|
||||
|
||||
@Post(':id/generate-link')
|
||||
@ApiOperation({
|
||||
summary: 'Generate sharable sale invoice link (private or public)',
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 201,
|
||||
description: 'The link has been generated successfully.',
|
||||
schema: {
|
||||
$ref: getSchemaPath(GenerateSaleInvoiceSharableLinkResponseDto),
|
||||
},
|
||||
})
|
||||
@ApiParam({
|
||||
name: 'id',
|
||||
required: true,
|
||||
type: Number,
|
||||
description: 'The sale invoice id',
|
||||
})
|
||||
generateSaleInvoiceSharableLink(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.saleInvoiceApplication.generateSaleInvoiceSharableLink(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import { events } from '@/common/events/events';
|
||||
import { PaymentLink } from '@/modules/PaymentLinks/models/PaymentLink';
|
||||
import { SaleInvoice } from '../models/SaleInvoice';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
|
||||
|
||||
@Injectable()
|
||||
export class GenerateShareLink {
|
||||
@@ -16,12 +17,13 @@ export class GenerateShareLink {
|
||||
private uow: UnitOfWork,
|
||||
private eventPublisher: EventEmitter2,
|
||||
private transformer: TransformerInjectable,
|
||||
private tenancyContext: TenancyContext,
|
||||
|
||||
@Inject(SaleInvoice.name)
|
||||
private saleInvoiceModel: TenantModelProxy<typeof SaleInvoice>,
|
||||
|
||||
@Inject(PaymentLink.name)
|
||||
private paymentLinkModel: TenantModelProxy<typeof PaymentLink>,
|
||||
private paymentLinkModel: typeof PaymentLink,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -39,10 +41,10 @@ export class GenerateShareLink {
|
||||
.query()
|
||||
.findById(saleInvoiceId)
|
||||
.throwIfNotFound();
|
||||
const tenant = await this.tenancyContext.getTenant();
|
||||
|
||||
// Generate unique uuid for sharable link.
|
||||
const linkId = uuidv4() as string;
|
||||
|
||||
const commonEventPayload = {
|
||||
saleInvoiceId,
|
||||
publicity,
|
||||
@@ -54,11 +56,12 @@ export class GenerateShareLink {
|
||||
events.saleInvoice.onPublicLinkGenerating,
|
||||
{ ...commonEventPayload, trx },
|
||||
);
|
||||
const paymentLink = await this.paymentLinkModel().query().insert({
|
||||
const paymentLink = await this.paymentLinkModel.query().insert({
|
||||
linkId,
|
||||
publicity,
|
||||
resourceId: foundInvoice.id,
|
||||
resourceType: 'SaleInvoice',
|
||||
tenantId: tenant.id,
|
||||
});
|
||||
// Triggers `onPublicSharableLinkGenerated` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class GenerateSaleInvoiceSharableLinkResponseDto {
|
||||
@ApiProperty({
|
||||
description: 'Sharable payment link for the sale invoice',
|
||||
example:
|
||||
'http://localhost:3000/payment/123e4567-e89b-12d3-a456-426614174000',
|
||||
})
|
||||
link: string;
|
||||
}
|
||||
Reference in New Issue
Block a user