update invoice entity
This commit is contained in:
@@ -40,9 +40,15 @@ export class CreateInvoiceItemDto {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class CreateInvoiceDto {
|
export class CreateInvoiceDto {
|
||||||
|
@IsOptional()
|
||||||
@IsString()
|
@IsString()
|
||||||
@ApiProperty({ description: 'Request ID to create invoice for' })
|
@ApiPropertyOptional({ description: 'Request ID to link invoice to (optional)' })
|
||||||
requestId!: string;
|
requestId?: string;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsString()
|
||||||
|
@ApiPropertyOptional({ description: 'User ID (required when requestId is not provided)' })
|
||||||
|
userId?: string;
|
||||||
|
|
||||||
@IsArray()
|
@IsArray()
|
||||||
@ArrayMinSize(1)
|
@ArrayMinSize(1)
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import { Request } from 'src/modules/request/entities/request.entity';
|
|||||||
@Entity({ tableName: 'invoices' })
|
@Entity({ tableName: 'invoices' })
|
||||||
@Index({ properties: ['user'] })
|
@Index({ properties: ['user'] })
|
||||||
export class Invoice extends BaseEntity {
|
export class Invoice extends BaseEntity {
|
||||||
[OptionalProps]?: 'createdAt' | 'deletedAt' | 'invoiceNumber'
|
[OptionalProps]?: 'createdAt' | 'deletedAt' | 'invoiceNumber' | 'request'
|
||||||
|
|
||||||
@PrimaryKey({ type: 'string', columnType: 'char(26)' })
|
@PrimaryKey({ type: 'string', columnType: 'char(26)' })
|
||||||
id: string = ulid()
|
id: string = ulid()
|
||||||
@@ -30,8 +30,8 @@ export class Invoice extends BaseEntity {
|
|||||||
@ManyToOne(() => User)
|
@ManyToOne(() => User)
|
||||||
user!: User;
|
user!: User;
|
||||||
|
|
||||||
@ManyToOne(() => Request)
|
@ManyToOne(() => Request, { nullable: true })
|
||||||
request!: Request;
|
request?: Request;
|
||||||
|
|
||||||
@OneToMany(() => Payment, payment => payment.invoice, {
|
@OneToMany(() => Payment, payment => payment.invoice, {
|
||||||
cascade: [Cascade.ALL],
|
cascade: [Cascade.ALL],
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export class InvoiceController {
|
|||||||
|
|
||||||
@Get('public/invoice/item/:id')
|
@Get('public/invoice/item/:id')
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
@ApiOperation({ summary: 'Get one invoice item by id' })
|
@ApiOperation({ summary: 'Confirm invoice item by id' })
|
||||||
confirmInvoiceItem(@Param('id') id: string,@UserId() userId: string) {
|
confirmInvoiceItem(@Param('id') id: string,@UserId() userId: string) {
|
||||||
return this.invoiceService.confirmInvoiceItem(id,userId);
|
return this.invoiceService.confirmInvoiceItem(id,userId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { FindInvoicesDto } from './dto/find-invoices.dto';
|
|||||||
import { Invoice } from './entities/invoice.entity';
|
import { Invoice } from './entities/invoice.entity';
|
||||||
import { InvoiceItem } from './entities/invoice-item.entity';
|
import { InvoiceItem } from './entities/invoice-item.entity';
|
||||||
import { Request } from '../request/entities/request.entity';
|
import { Request } from '../request/entities/request.entity';
|
||||||
|
import { User } from '../user/entities/user.entity';
|
||||||
import { ProductService } from '../product/providers/product.service';
|
import { ProductService } from '../product/providers/product.service';
|
||||||
import { InvoiceRepository } from './repositories/invoice.repository';
|
import { InvoiceRepository } from './repositories/invoice.repository';
|
||||||
|
|
||||||
@@ -21,13 +22,24 @@ export class InvoiceService {
|
|||||||
|
|
||||||
|
|
||||||
async create(dto: CreateInvoiceDto) {
|
async create(dto: CreateInvoiceDto) {
|
||||||
const request = await this.em.findOne(
|
let user: User;
|
||||||
Request,
|
let request: Request | undefined;
|
||||||
{ id: dto.requestId },
|
|
||||||
{ populate: ['user'] },
|
if (dto.requestId) {
|
||||||
);
|
const requestEntity = await this.em.findOne(
|
||||||
if (!request) {
|
Request,
|
||||||
throw new NotFoundException('Request not found');
|
{ id: dto.requestId },
|
||||||
|
{ populate: ['user'] },
|
||||||
|
);
|
||||||
|
if (!requestEntity) {
|
||||||
|
throw new NotFoundException('Request not found');
|
||||||
|
}
|
||||||
|
user = requestEntity.user;
|
||||||
|
request = requestEntity;
|
||||||
|
} else if (dto.userId) {
|
||||||
|
user = await this.em.findOneOrFail(User, { id: dto.userId });
|
||||||
|
} else {
|
||||||
|
throw new BadRequestException('Either requestId or userId must be provided');
|
||||||
}
|
}
|
||||||
|
|
||||||
const enableTax = dto.enableTax ?? false;
|
const enableTax = dto.enableTax ?? false;
|
||||||
@@ -39,8 +51,8 @@ export class InvoiceService {
|
|||||||
|
|
||||||
return this.em.transactional(async (em) => {
|
return this.em.transactional(async (em) => {
|
||||||
const invoice = em.create(Invoice, {
|
const invoice = em.create(Invoice, {
|
||||||
user: request.user,
|
user,
|
||||||
request,
|
...(request && { request }),
|
||||||
enableTax,
|
enableTax,
|
||||||
approvalDeadline,
|
approvalDeadline,
|
||||||
discount,
|
discount,
|
||||||
|
|||||||
Reference in New Issue
Block a user