Uecko_ERP/modules/customers/src/api/infrastructure/sequelize/customer.model.ts
2025-09-02 10:57:41 +02:00

196 lines
4.2 KiB
TypeScript

import { DataTypes, InferAttributes, InferCreationAttributes, Model, Sequelize } from "sequelize";
export type CustomerCreationAttributes = InferCreationAttributes<CustomerModel, {}> & {};
export class CustomerModel extends Model<
InferAttributes<CustomerModel>,
InferCreationAttributes<CustomerModel>
> {
// To avoid table creation
/*static async sync(): Promise<any> {
return Promise.resolve();
}*/
declare id: string;
declare company_id: string;
declare reference: string;
declare is_company: boolean;
declare name: string;
declare trade_name: string;
declare tin: string;
declare street: string;
declare street2: string;
declare city: string;
declare province: string;
declare postal_code: string;
declare country: string;
declare email: string;
declare phone: string;
declare fax: string;
declare website: string;
declare legal_record: string;
declare default_taxes: string;
declare status: string;
declare language_code: string;
declare currency_code: string;
static associate(database: Sequelize) {}
static hooks(database: Sequelize) {}
}
export default (database: Sequelize) => {
CustomerModel.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
},
company_id: {
type: DataTypes.UUID,
allowNull: false,
},
reference: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
},
is_company: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
trade_name: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
},
tin: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
},
street: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
},
street2: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
},
city: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
},
province: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
},
postal_code: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
},
country: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
},
email: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
validate: {
isEmail: true,
},
},
phone: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
},
fax: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
},
website: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
validate: {
isUrl: true,
},
},
legal_record: {
type: DataTypes.TEXT,
defaultValue: null,
allowNull: true,
},
default_taxes: {
type: DataTypes.STRING,
defaultValue: null,
allowNull: true,
},
language_code: {
type: DataTypes.STRING(2),
allowNull: false,
defaultValue: "es",
},
currency_code: {
type: new DataTypes.STRING(3),
allowNull: false,
defaultValue: "EUR",
},
status: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: "active",
},
},
{
sequelize: database,
tableName: "customers",
paranoid: true, // softs deletes
timestamps: true,
createdAt: "created_at",
updatedAt: "updated_at",
deletedAt: "deleted_at",
indexes: [
{ name: "company_idx", fields: ["company_id"], unique: false },
{ name: "idx_company_idx", fields: ["id", "company_id"], unique: true },
{ name: "email_idx", fields: ["email"], unique: true },
],
whereMergeStrategy: "and", // <- cómo tratar el merge de un scope
defaultScope: {},
scopes: {},
}
);
return CustomerModel;
};