104 lines
2.2 KiB
TypeScript
104 lines
2.2 KiB
TypeScript
|
|
import {
|
||
|
|
DataTypes,
|
||
|
|
InferAttributes,
|
||
|
|
InferCreationAttributes,
|
||
|
|
Model,
|
||
|
|
Op,
|
||
|
|
Sequelize,
|
||
|
|
} from "sequelize";
|
||
|
|
|
||
|
|
export enum DealerStatus {
|
||
|
|
ACTIVE = "active",
|
||
|
|
BLOCKED = "blocked",
|
||
|
|
}
|
||
|
|
|
||
|
|
export type DealerCreationAttributes = InferCreationAttributes<Dealer_Model>;
|
||
|
|
|
||
|
|
export class Dealer_Model extends Model<
|
||
|
|
InferAttributes<Dealer_Model>,
|
||
|
|
InferCreationAttributes<Dealer_Model>
|
||
|
|
> {
|
||
|
|
// To avoid table creation
|
||
|
|
/*static async sync(): Promise<any> {
|
||
|
|
return Promise.resolve();
|
||
|
|
}*/
|
||
|
|
|
||
|
|
static associate(connection: Sequelize) {}
|
||
|
|
|
||
|
|
declare id: string;
|
||
|
|
declare id_contact?: string; // number ??
|
||
|
|
declare name: string;
|
||
|
|
declare contact_information: string;
|
||
|
|
declare default_payment_method: string;
|
||
|
|
declare default_notes: string;
|
||
|
|
declare default_legal_terms: string;
|
||
|
|
declare default_quote_validity: string;
|
||
|
|
declare status: DealerStatus;
|
||
|
|
}
|
||
|
|
|
||
|
|
export default (sequelize: Sequelize) => {
|
||
|
|
Dealer_Model.init(
|
||
|
|
{
|
||
|
|
id: {
|
||
|
|
type: new DataTypes.UUID(),
|
||
|
|
primaryKey: true,
|
||
|
|
},
|
||
|
|
|
||
|
|
id_contact: {
|
||
|
|
type: DataTypes.BIGINT().UNSIGNED,
|
||
|
|
allowNull: true,
|
||
|
|
},
|
||
|
|
|
||
|
|
name: {
|
||
|
|
type: DataTypes.STRING,
|
||
|
|
allowNull: false,
|
||
|
|
},
|
||
|
|
|
||
|
|
contact_information: DataTypes.STRING,
|
||
|
|
default_payment_method: DataTypes.STRING,
|
||
|
|
default_notes: DataTypes.STRING,
|
||
|
|
default_legal_terms: DataTypes.STRING,
|
||
|
|
default_quote_validity: DataTypes.STRING,
|
||
|
|
|
||
|
|
status: {
|
||
|
|
type: DataTypes.ENUM(...Object.values(DealerStatus)),
|
||
|
|
allowNull: false,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
sequelize,
|
||
|
|
tableName: "dealers",
|
||
|
|
|
||
|
|
paranoid: true, // softs deletes
|
||
|
|
timestamps: true,
|
||
|
|
//version: true,
|
||
|
|
|
||
|
|
createdAt: "created_at",
|
||
|
|
updatedAt: "updated_at",
|
||
|
|
deletedAt: "deleted_at",
|
||
|
|
|
||
|
|
indexes: [
|
||
|
|
{ name: "id_contact_idx", fields: ["id_contact"] },
|
||
|
|
{ name: "status_idx", fields: ["status"] },
|
||
|
|
],
|
||
|
|
|
||
|
|
whereMergeStrategy: "and", // <- cómo tratar el merge de un scope
|
||
|
|
scopes: {
|
||
|
|
quickSearch(value) {
|
||
|
|
return {
|
||
|
|
where: {
|
||
|
|
[Op.or]: {
|
||
|
|
name: {
|
||
|
|
[Op.like]: `%${value}%`,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
};
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
return Dealer_Model;
|
||
|
|
};
|