2025-09-26 18:09:14 +00:00
|
|
|
import {
|
2026-03-16 17:45:45 +00:00
|
|
|
type CreationOptional,
|
2025-09-26 18:09:14 +00:00
|
|
|
DataTypes,
|
2026-03-16 17:45:45 +00:00
|
|
|
type InferAttributes,
|
|
|
|
|
type InferCreationAttributes,
|
2025-09-26 18:09:14 +00:00
|
|
|
Model,
|
2026-03-16 17:45:45 +00:00
|
|
|
type Sequelize,
|
2025-09-26 18:09:14 +00:00
|
|
|
} 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-09-26 18:09:14 +00:00
|
|
|
declare reference: CreationOptional<string | null>;
|
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-09-26 18:09:14 +00:00
|
|
|
declare trade_name: CreationOptional<string | null>;
|
|
|
|
|
declare tin: CreationOptional<string | null>;
|
2025-08-11 17:49:52 +00:00
|
|
|
|
2025-09-26 18:09:14 +00:00
|
|
|
declare street: CreationOptional<string | null>;
|
|
|
|
|
declare street2: CreationOptional<string | null>;
|
|
|
|
|
declare city: CreationOptional<string | null>;
|
|
|
|
|
declare province: CreationOptional<string | null>;
|
|
|
|
|
declare postal_code: CreationOptional<string | null>;
|
|
|
|
|
declare country: CreationOptional<string | null>;
|
2025-08-11 17:49:52 +00:00
|
|
|
|
2025-09-16 09:17:29 +00:00
|
|
|
// Correos electrónicos
|
2025-09-26 18:09:14 +00:00
|
|
|
declare email_primary: CreationOptional<string | null>;
|
|
|
|
|
declare email_secondary: CreationOptional<string | null>;
|
2025-09-16 09:17:29 +00:00
|
|
|
|
|
|
|
|
// Teléfonos fijos
|
2025-09-26 18:09:14 +00:00
|
|
|
declare phone_primary: CreationOptional<string | null>;
|
|
|
|
|
declare phone_secondary: CreationOptional<string | null>;
|
2025-09-16 09:17:29 +00:00
|
|
|
|
|
|
|
|
// Móviles
|
2025-09-26 18:09:14 +00:00
|
|
|
declare mobile_primary: CreationOptional<string | null>;
|
|
|
|
|
declare mobile_secondary: CreationOptional<string | null>;
|
2025-09-16 09:17:29 +00:00
|
|
|
|
2025-09-26 18:09:14 +00:00
|
|
|
declare fax: CreationOptional<string | null>;
|
|
|
|
|
declare website: CreationOptional<string | null>;
|
2025-08-11 17:49:52 +00:00
|
|
|
|
2025-09-26 18:09:14 +00:00
|
|
|
declare legal_record: CreationOptional<string | null>;
|
2025-08-11 17:49:52 +00:00
|
|
|
|
2026-03-25 09:34:17 +00:00
|
|
|
declare default_taxes: string;
|
2025-08-11 17:49:52 +00:00
|
|
|
declare status: string;
|
2025-09-26 18:09:14 +00:00
|
|
|
declare language_code: CreationOptional<string>;
|
|
|
|
|
declare currency_code: CreationOptional<string>;
|
2025-08-25 17:42:56 +00:00
|
|
|
|
2025-11-05 10:42:20 +00:00
|
|
|
// FactuGES
|
2025-09-26 18:09:14 +00:00
|
|
|
declare factuges_id: CreationOptional<string | null>;
|
2025-09-10 18:14:19 +00:00
|
|
|
|
2025-10-30 18:26:57 +00:00
|
|
|
static associate(_database: Sequelize) {}
|
2025-08-25 17:42:56 +00:00
|
|
|
|
2025-10-30 18:26:57 +00:00
|
|
|
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,
|
2025-09-02 08:57:41 +00:00
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
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-09-02 08:57:41 +00:00
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
2025-08-11 17:49:52 +00:00
|
|
|
},
|
|
|
|
|
tin: {
|
|
|
|
|
type: DataTypes.STRING,
|
2025-09-02 08:57:41 +00:00
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
2025-08-11 17:49:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
street: {
|
|
|
|
|
type: DataTypes.STRING,
|
2025-09-02 08:57:41 +00:00
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
2025-08-26 18:55:59 +00:00
|
|
|
},
|
|
|
|
|
street2: {
|
|
|
|
|
type: DataTypes.STRING,
|
2025-09-02 08:57:41 +00:00
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
2025-08-11 17:49:52 +00:00
|
|
|
},
|
|
|
|
|
city: {
|
|
|
|
|
type: DataTypes.STRING,
|
2025-09-02 08:57:41 +00:00
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
2025-08-11 17:49:52 +00:00
|
|
|
},
|
2025-09-01 14:07:59 +00:00
|
|
|
province: {
|
2025-08-11 17:49:52 +00:00
|
|
|
type: DataTypes.STRING,
|
2025-09-02 08:57:41 +00:00
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
2025-08-11 17:49:52 +00:00
|
|
|
},
|
|
|
|
|
postal_code: {
|
|
|
|
|
type: DataTypes.STRING,
|
2025-09-02 08:57:41 +00:00
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
2025-08-11 17:49:52 +00:00
|
|
|
},
|
|
|
|
|
country: {
|
|
|
|
|
type: DataTypes.STRING,
|
2025-09-02 08:57:41 +00:00
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
2025-08-11 17:49:52 +00:00
|
|
|
},
|
|
|
|
|
|
2025-09-16 09:17:29 +00:00
|
|
|
email_primary: {
|
2025-08-11 17:49:52 +00:00
|
|
|
type: DataTypes.STRING,
|
2025-09-02 08:57:41 +00:00
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
2025-08-11 17:49:52 +00:00
|
|
|
validate: {
|
|
|
|
|
isEmail: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-09-16 09:17:29 +00:00
|
|
|
email_secondary: {
|
2025-08-11 17:49:52 +00:00
|
|
|
type: DataTypes.STRING,
|
2025-09-02 08:57:41 +00:00
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
2025-09-16 09:17:29 +00:00
|
|
|
validate: {
|
|
|
|
|
isEmail: true,
|
|
|
|
|
},
|
2025-08-11 17:49:52 +00:00
|
|
|
},
|
2025-09-16 09:17:29 +00:00
|
|
|
|
|
|
|
|
phone_primary: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
|
|
|
|
},
|
|
|
|
|
phone_secondary: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
mobile_primary: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
|
|
|
|
},
|
|
|
|
|
mobile_secondary: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
|
|
|
|
},
|
|
|
|
|
|
2025-08-11 17:49:52 +00:00
|
|
|
fax: {
|
|
|
|
|
type: DataTypes.STRING,
|
2025-09-02 08:57:41 +00:00
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
2025-08-11 17:49:52 +00:00
|
|
|
},
|
|
|
|
|
website: {
|
|
|
|
|
type: DataTypes.STRING,
|
2025-09-02 08:57:41 +00:00
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
2025-08-11 17:49:52 +00:00
|
|
|
validate: {
|
|
|
|
|
isUrl: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
legal_record: {
|
|
|
|
|
type: DataTypes.TEXT,
|
2025-09-02 08:57:41 +00:00
|
|
|
defaultValue: null,
|
|
|
|
|
allowNull: true,
|
2025-08-11 17:49:52 +00:00
|
|
|
},
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
default_taxes: {
|
2025-08-26 18:55:59 +00:00
|
|
|
type: DataTypes.STRING,
|
2025-09-01 14:07:59 +00:00
|
|
|
defaultValue: null,
|
2025-09-02 08:57:41 +00:00
|
|
|
allowNull: true,
|
2025-08-11 17:49:52 +00:00
|
|
|
},
|
|
|
|
|
|
2025-09-26 18:09:14 +00:00
|
|
|
status: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: false,
|
|
|
|
|
defaultValue: "active",
|
|
|
|
|
},
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
language_code: {
|
2025-08-11 17:49:52 +00:00
|
|
|
type: DataTypes.STRING(2),
|
|
|
|
|
allowNull: false,
|
|
|
|
|
defaultValue: "es",
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
currency_code: {
|
|
|
|
|
type: new DataTypes.STRING(3),
|
|
|
|
|
allowNull: false,
|
|
|
|
|
defaultValue: "EUR",
|
|
|
|
|
},
|
|
|
|
|
|
2025-09-10 18:14:19 +00:00
|
|
|
factuges_id: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: true,
|
|
|
|
|
defaultValue: null,
|
|
|
|
|
},
|
2025-08-11 17:49:52 +00:00
|
|
|
},
|
|
|
|
|
{
|
2025-08-25 17:42:56 +00:00
|
|
|
sequelize: database,
|
2025-10-30 18:26:57 +00:00
|
|
|
modelName: "CustomerModel",
|
2025-08-11 17:49:52 +00:00
|
|
|
tableName: "customers",
|
|
|
|
|
|
2025-09-03 10:41:12 +00:00
|
|
|
underscored: true,
|
2025-08-11 17:49:52 +00:00
|
|
|
paranoid: true, // softs deletes
|
|
|
|
|
timestamps: true,
|
|
|
|
|
|
|
|
|
|
createdAt: "created_at",
|
|
|
|
|
updatedAt: "updated_at",
|
|
|
|
|
deletedAt: "deleted_at",
|
|
|
|
|
|
|
|
|
|
indexes: [
|
2025-10-24 11:01:51 +00:00
|
|
|
{
|
|
|
|
|
name: "idx_customers_company_name",
|
|
|
|
|
fields: ["company_id", "deleted_at", "name"],
|
|
|
|
|
},
|
|
|
|
|
{ name: "idx_name", fields: ["name"] }, // <- para ordenación
|
2026-03-16 17:45:45 +00:00
|
|
|
{ name: "idx_tin", fields: ["tin"] }, // <- para servicios externos
|
2025-10-24 11:01:51 +00:00
|
|
|
{ name: "idx_company_idx", fields: ["id", "company_id"], unique: true }, // <- para consulta get
|
2025-11-05 10:42:20 +00:00
|
|
|
{ name: "idx_factuges", fields: ["factuges_id"], unique: true }, // <- para el proceso python
|
|
|
|
|
|
2025-09-30 10:59:32 +00:00
|
|
|
{
|
|
|
|
|
name: "ft_customer",
|
|
|
|
|
type: "FULLTEXT",
|
|
|
|
|
fields: ["name", "trade_name", "reference", "tin", "email_primary", "mobile_primary"],
|
|
|
|
|
},
|
2025-08-11 17:49:52 +00:00
|
|
|
],
|
|
|
|
|
|
|
|
|
|
whereMergeStrategy: "and", // <- cómo tratar el merge de un scope
|
|
|
|
|
|
|
|
|
|
defaultScope: {},
|
|
|
|
|
|
|
|
|
|
scopes: {},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
return CustomerModel;
|
|
|
|
|
};
|