Uecko_ERP/apps/server/archive/contexts/contacts/infraestructure/sequelize/contact.model.ts

176 lines
3.6 KiB
TypeScript
Raw Normal View History

2025-02-20 18:55:24 +00:00
import {
CreationOptional,
DataTypes,
InferAttributes,
InferCreationAttributes,
Model,
Sequelize,
} from "sequelize";
2025-02-25 17:47:42 +00:00
export type ContactCreationAttributes = InferCreationAttributes<ContactModel, {}> & {};
2025-02-20 18:55:24 +00:00
2025-02-25 17:47:42 +00:00
export class ContactModel extends Model<
InferAttributes<ContactModel>,
InferCreationAttributes<ContactModel>
2025-02-20 18:55:24 +00:00
> {
// To avoid table creation
/*static async sync(): Promise<any> {
return Promise.resolve();
}*/
declare id: string;
2025-02-25 17:47:42 +00:00
declare reference: CreationOptional<string>;
2025-02-20 18:55:24 +00:00
2025-08-25 17:42:56 +00:00
declare is_companyr: boolean;
2025-02-20 18:55:24 +00:00
declare name: string;
declare trade_name: CreationOptional<string>;
declare tin: string;
declare street: string;
declare city: string;
declare state: string;
declare postal_code: string;
declare country: string;
declare email: string;
declare phone: string;
declare fax: CreationOptional<string>;
declare website: CreationOptional<string>;
declare legal_record: string;
declare default_tax: number;
declare status: string;
2025-09-18 11:17:18 +00:00
declare language_code: string;
2025-02-20 18:55:24 +00:00
declare currency_code: string;
}
export default (sequelize: Sequelize) => {
2025-02-25 17:47:42 +00:00
ContactModel.init(
2025-02-20 18:55:24 +00:00
{
id: {
type: DataTypes.UUID,
primaryKey: true,
},
2025-02-25 17:47:42 +00:00
reference: {
type: DataTypes.STRING,
allowNull: false,
},
2025-08-25 17:42:56 +00:00
is_companyr: {
2025-02-20 18:55:24 +00:00
type: DataTypes.BOOLEAN,
allowNull: false,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
trade_name: {
type: DataTypes.STRING,
allowNull: true,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-20 18:55:24 +00:00
},
tin: {
type: DataTypes.STRING,
allowNull: false,
},
street: {
type: DataTypes.STRING,
allowNull: false,
},
city: {
type: DataTypes.STRING,
allowNull: false,
},
state: {
type: DataTypes.STRING,
allowNull: false,
},
postal_code: {
type: DataTypes.STRING,
allowNull: false,
},
country: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isEmail: true,
},
},
phone: {
type: DataTypes.STRING,
allowNull: false,
},
fax: {
type: DataTypes.STRING,
allowNull: true,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-20 18:55:24 +00:00
},
website: {
type: DataTypes.STRING,
allowNull: true,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-20 18:55:24 +00:00
validate: {
isUrl: true,
},
},
legal_record: {
type: DataTypes.TEXT,
allowNull: false,
},
default_tax: {
type: new DataTypes.SMALLINT(),
allowNull: false,
defaultValue: 2100,
},
2025-09-18 11:17:18 +00:00
language_code: {
2025-02-20 18:55:24 +00:00
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,
2025-02-25 17:47:42 +00:00
tableName: "contacts",
2025-02-20 18:55:24 +00:00
paranoid: true, // softs deletes
timestamps: true,
createdAt: "created_at",
updatedAt: "updated_at",
deletedAt: "deleted_at",
2025-02-25 17:47:42 +00:00
indexes: [
{ name: "email_idx", fields: ["email"], unique: true },
{ name: "reference_idx", fields: ["reference"], unique: true },
],
2025-02-20 18:55:24 +00:00
whereMergeStrategy: "and", // <- cómo tratar el merge de un scope
defaultScope: {},
scopes: {},
}
);
2025-02-25 17:47:42 +00:00
return ContactModel;
2025-02-20 18:55:24 +00:00
};