Presupuestador_web/server/src/contexts/sales/infrastructure/sequelize/quote.model.ts

104 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-05-26 17:09:43 +00:00
import {
CreationOptional,
DataTypes,
InferAttributes,
InferCreationAttributes,
Model,
NonAttribute,
Sequelize,
} from "sequelize";
import { QuoteItem_Model } from "./quoteItem.model";
export type QuoteCreationAttributes = InferCreationAttributes<Quote_Model, { omit: "items" }>;
export class Quote_Model extends Model<
InferAttributes<Quote_Model, { omit: "items" }>,
InferCreationAttributes<Quote_Model, { omit: "items" }>
> {
static associate(connection: Sequelize) {
const { Quote_Model, QuoteItem_Model, Dealer_Model } = connection.models;
Quote_Model.hasMany(QuoteItem_Model, {
as: "items",
foreignKey: "quote_id",
onDelete: "CASCADE",
});
Quote_Model.belongsTo(Dealer_Model, {
as: "dealer",
foreignKey: "dealer_id",
onDelete: "RESTRICT",
});
}
declare id: string;
declare status: string;
declare date: CreationOptional<string>;
declare language_code: string;
declare currency_code: string;
declare subtotal: number;
declare total: number;
declare items: NonAttribute<QuoteItem_Model[]>;
}
export default (sequelize: Sequelize) => {
Quote_Model.init(
{
id: {
type: new DataTypes.UUID(),
primaryKey: true,
},
status: {
type: new DataTypes.STRING(),
allowNull: false,
},
date: {
type: new DataTypes.DATE(),
allowNull: false,
},
language_code: {
type: new DataTypes.STRING(),
allowNull: false,
},
currency_code: {
type: new DataTypes.STRING(),
allowNull: false,
},
subtotal: {
type: new DataTypes.BIGINT(),
allowNull: true,
},
total: {
type: new DataTypes.BIGINT(),
allowNull: true,
},
},
{
sequelize,
tableName: "quotes",
paranoid: true, // softs deletes
timestamps: true,
//version: true,
createdAt: "created_at",
updatedAt: "updated_at",
deletedAt: "deleted_at",
indexes: [
{ name: "status_idx", fields: ["status"] },
{ name: "deleted_at_idx", fields: ["deleted_at"] },
],
}
);
return Quote_Model;
};