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