Uecko_ERP/modules/customers/src/api/infrastructure/sequelize/customer.model.ts

195 lines
4.1 KiB
TypeScript
Raw Normal View History

2025-08-26 18:55:59 +00:00
import { DataTypes, InferAttributes, InferCreationAttributes, Model, Sequelize } from "sequelize";
2025-08-11 17:49:52 +00:00
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;
2025-08-25 17:42:56 +00:00
declare company_id: string;
2025-08-26 18:55:59 +00:00
declare reference: string;
2025-08-11 17:49:52 +00:00
2025-08-25 17:42:56 +00:00
declare is_company: boolean;
2025-08-11 17:49:52 +00:00
declare name: string;
2025-08-26 18:55:59 +00:00
declare trade_name: string;
2025-08-11 17:49:52 +00:00
declare tin: string;
declare street: string;
2025-08-26 18:55:59 +00:00
declare street2: string;
2025-08-11 17:49:52 +00:00
declare city: string;
declare state: string;
declare postal_code: string;
declare country: string;
declare email: string;
declare phone: string;
2025-08-26 18:55:59 +00:00
declare fax: string;
declare website: string;
2025-08-11 17:49:52 +00:00
declare legal_record: string;
2025-08-26 18:55:59 +00:00
declare default_tax: string;
2025-08-11 17:49:52 +00:00
declare status: string;
declare lang_code: string;
declare currency_code: string;
2025-08-25 17:42:56 +00:00
static associate(database: Sequelize) {}
static hooks(database: Sequelize) {}
2025-08-11 17:49:52 +00:00
}
2025-08-25 17:42:56 +00:00
export default (database: Sequelize) => {
2025-08-11 17:49:52 +00:00
CustomerModel.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
},
2025-08-25 17:42:56 +00:00
company_id: {
type: DataTypes.UUID,
allowNull: false,
},
2025-08-11 17:49:52 +00:00
reference: {
type: DataTypes.STRING,
allowNull: false,
2025-08-26 18:55:59 +00:00
defaultValue: "",
2025-08-11 17:49:52 +00:00
},
2025-08-25 17:42:56 +00:00
is_company: {
2025-08-11 17:49:52 +00:00
type: DataTypes.BOOLEAN,
allowNull: false,
2025-08-26 18:55:59 +00:00
defaultValue: false,
2025-08-11 17:49:52 +00:00
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
trade_name: {
type: DataTypes.STRING,
2025-08-26 18:55:59 +00:00
allowNull: false,
defaultValue: "",
2025-08-11 17:49:52 +00:00
},
tin: {
type: DataTypes.STRING,
allowNull: false,
2025-08-26 18:55:59 +00:00
defaultValue: "",
2025-08-11 17:49:52 +00:00
},
street: {
type: DataTypes.STRING,
allowNull: false,
2025-08-26 18:55:59 +00:00
defaultValue: "",
},
street2: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: "",
2025-08-11 17:49:52 +00:00
},
city: {
type: DataTypes.STRING,
allowNull: false,
2025-08-26 18:55:59 +00:00
defaultValue: "",
2025-08-11 17:49:52 +00:00
},
state: {
type: DataTypes.STRING,
allowNull: false,
2025-08-26 18:55:59 +00:00
defaultValue: "",
2025-08-11 17:49:52 +00:00
},
postal_code: {
type: DataTypes.STRING,
allowNull: false,
2025-08-26 18:55:59 +00:00
defaultValue: "",
2025-08-11 17:49:52 +00:00
},
country: {
type: DataTypes.STRING,
allowNull: false,
2025-08-26 18:55:59 +00:00
defaultValue: "",
2025-08-11 17:49:52 +00:00
},
email: {
type: DataTypes.STRING,
allowNull: false,
2025-08-26 18:55:59 +00:00
defaultValue: "",
2025-08-11 17:49:52 +00:00
validate: {
isEmail: true,
},
},
phone: {
type: DataTypes.STRING,
allowNull: false,
2025-08-26 18:55:59 +00:00
defaultValue: "",
2025-08-11 17:49:52 +00:00
},
fax: {
type: DataTypes.STRING,
2025-08-26 18:55:59 +00:00
allowNull: false,
defaultValue: "",
2025-08-11 17:49:52 +00:00
},
website: {
type: DataTypes.STRING,
2025-08-26 18:55:59 +00:00
allowNull: false,
defaultValue: "",
2025-08-11 17:49:52 +00:00
validate: {
isUrl: true,
},
},
legal_record: {
type: DataTypes.TEXT,
allowNull: false,
2025-08-26 18:55:59 +00:00
defaultValue: "",
2025-08-11 17:49:52 +00:00
},
default_tax: {
2025-08-26 18:55:59 +00:00
type: DataTypes.STRING,
2025-08-11 17:49:52 +00:00
allowNull: false,
2025-08-26 18:55:59 +00:00
defaultValue: "",
2025-08-11 17:49:52 +00:00
},
lang_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",
},
},
{
2025-08-25 17:42:56 +00:00
sequelize: database,
2025-08-11 17:49:52 +00:00
tableName: "customers",
paranoid: true, // softs deletes
timestamps: true,
createdAt: "created_at",
updatedAt: "updated_at",
deletedAt: "deleted_at",
indexes: [
2025-08-25 17:42:56 +00:00
{ name: "company_idx", fields: ["company_id"], unique: false },
2025-08-11 17:49:52 +00:00
{ name: "email_idx", fields: ["email"], unique: true },
],
whereMergeStrategy: "and", // <- cómo tratar el merge de un scope
defaultScope: {},
scopes: {},
}
);
return CustomerModel;
};