metis-desk/modules/companies/src/api/infrastructure/persistence/company.model.ts

73 lines
2.3 KiB
TypeScript

import type {
CreationOptional,
InferAttributes,
InferCreationAttributes,
Sequelize,
} from "sequelize";
import { DataTypes, Model } from "sequelize";
export class CompanyModel extends Model<
InferAttributes<CompanyModel>,
InferCreationAttributes<CompanyModel>
> {
declare id: string;
declare name: string;
declare active: boolean;
declare logoVersion: CreationOptional<string | null>;
}
export class PersonModel extends Model<
InferAttributes<PersonModel>,
InferCreationAttributes<PersonModel>
> {
declare id: string;
declare companyId: string;
declare name: string;
declare position: string | null;
declare email: string | null;
declare phone: string | null;
declare isContact: boolean;
declare active: boolean;
declare photoVersion: CreationOptional<string | null>;
}
export class SiteModel extends Model<
InferAttributes<SiteModel>,
InferCreationAttributes<SiteModel>
> {
declare id: string;
declare companyId: string;
declare name: string;
declare address: string | null;
}
export function initializeCompanyModels(database: Sequelize): void {
CompanyModel.init(
{
id: { type: DataTypes.UUID, primaryKey: true },
name: { type: DataTypes.STRING(100), allowNull: false },
active: { type: DataTypes.BOOLEAN, allowNull: false },
logoVersion: { type: DataTypes.STRING(64), allowNull: true },
},
{ sequelize: database, tableName: "companies", timestamps: false }
);
const childFields = {
id: { type: DataTypes.UUID, primaryKey: true },
companyId: { type: DataTypes.UUID, allowNull: false },
name: { type: DataTypes.STRING(100), allowNull: false },
};
PersonModel.init(
{
...childFields,
position: { type: DataTypes.STRING(100), allowNull: true },
email: { type: DataTypes.STRING(254), allowNull: true },
phone: { type: DataTypes.STRING(50), allowNull: true },
isContact: { type: DataTypes.BOOLEAN, allowNull: false },
active: { type: DataTypes.BOOLEAN, allowNull: false },
photoVersion: { type: DataTypes.STRING(64), allowNull: true },
},
{ sequelize: database, tableName: "company_contacts", timestamps: false }
);
SiteModel.init(
{ ...childFields, address: { type: DataTypes.TEXT, allowNull: true } },
{ sequelize: database, tableName: "company_sites", timestamps: false }
);
}