Skip to content

Instantly share code, notes, and snippets.

@Kratosgado
Last active September 30, 2024 13:34
Show Gist options
  • Select an option

  • Save Kratosgado/cf5a2418384e2054b4ddfd4b6e832def to your computer and use it in GitHub Desktop.

Select an option

Save Kratosgado/cf5a2418384e2054b4ddfd4b6e832def to your computer and use it in GitHub Desktop.
I added foreign keys to the existing drug model, user model,
import {
BelongsTo,
Column,
DataType,
ForeignKey,
Index,
Table,
} from 'sequelize-typescript';
import { DrugsCategory } from 'src/inventory/drugs-category/models/drugs-category.model';
import { Department, Facility } from 'src/inventory/models/inventory.model';
import { Supplier } from 'src/inventory/suppliers/models/supplier.model';
import { BaseModel } from 'src/shared/models/base.model';
@Table({
tableName: 'drugs',
underscored: true,
})
export class Drug extends BaseModel {
@Index
@Column
name: string;
@Column({ type: DataType.DOUBLE, allowNull: false, field: 'cost_price' })
costPrice: number;
@Column({ type: DataType.DOUBLE, allowNull: false, field: 'selling_price' })
sellingPrice: number;
@Column({ type: DataType.ENUM('SOLIDS', 'LIQUIDS'), field: 'dosage_form' })
dosageForm: string;
@Column
code: string;
@Column({ type: DataType.DATEONLY, allowNull: false })
validity: Date;
@Column
fdaApproval: string;
@Column
ISO: string;
@Column
batch: string;
@Column({ type: DataType.INTEGER, allowNull: false })
stock: number;
@Column({ type: DataType.INTEGER, allowNull: false, field: 'reorder_point' })
reorderPoint: number;
@Column
manufacturer: string;
@Column({
type: DataType.ENUM('LOW', 'STOCKED', 'OUT_OF_STOCK'),
allowNull: false,
})
status: string;
@Column({ type: DataType.TEXT, field: 'storage_req' })
storageReq: string;
@ForeignKey(() => DrugsCategory)
@Column({
type: DataType.UUID,
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
field: 'category_id',
})
categoryId: string;
@BelongsTo(() => DrugsCategory, 'category_id')
category: DrugsCategory;
@ForeignKey(() => Supplier)
@Column
supplierId: string;
@BelongsTo(() => Supplier)
supplier: Supplier;
@ForeignKey(() => Department)
@Column
departmentId: string;
@BelongsTo(() => Department, 'department_id')
department: Department;
@ForeignKey(() => Facility)
@Column
facilitId: string;
@BelongsTo(() => Facility, 'facility_id')
facility: Facility;
}
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/no-unused-vars */
'use strict';
const baseModelColumns = require('../migration-base.js');
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('facilities', {
...baseModelColumns,
name: { type: Sequelize.STRING },
region: { type: Sequelize.STRING },
location: { type: Sequelize.STRING },
});
await queryInterface.createTable('departments', {
...baseModelColumns,
name: { type: Sequelize.STRING },
facilityId: {
references: {
model: 'facilities',
key: 'id',
},
type: Sequelize.UUID,
field: 'facility_id',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
},
});
await queryInterface.addColumn('users', 'department_id', {
references: {
model: 'departments',
key: 'id',
},
type: Sequelize.UUID,
field: 'department_id',
});
await queryInterface.addColumn('users', 'facility_id', {
allowNull: true,
references: {
model: 'facilities',
key: 'id',
},
type: Sequelize.UUID,
field: 'facility_id',
});
await queryInterface.addColumn('drugs', 'facility_id', {
references: {
model: 'facilities',
key: 'id',
},
type: Sequelize.UUID,
field: 'facility_id',
});
await queryInterface.addColumn('drugs', 'department_id', {
allowNull: true,
references: {
model: 'departments',
key: 'id',
},
type: Sequelize.UUID,
field: 'department_id',
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('facilities');
await queryInterface.dropTable('departments');
await queryInterface.removeColumn('users', 'department_id');
await queryInterface.removeColumn('users', 'facility_id');
await queryInterface.removeColumn('drugs', 'facility_id');
await queryInterface.removeColumn('drugs', 'department_id');
},
};
import {
BelongsTo,
Column,
ForeignKey,
HasMany,
Table,
} from 'sequelize-typescript';
import { BaseModel } from 'src/shared/models/base.model';
import { User } from 'src/auth/models/user.model';
import { Drug } from '../drugs/models/drug.model';
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, IsUUID } from 'class-validator';
@Table({
tableName: 'facilities',
underscored: true,
})
export class Facility extends BaseModel {
@ApiProperty({
example: 'Hospital A',
description: 'The name of the hospital',
})
@IsString()
@IsNotEmpty()
@Column
name: string;
@ApiProperty({
example: 'North',
description: 'The region where the hospital is located',
})
@IsString()
@IsNotEmpty()
@Column
region: string;
@ApiProperty({
example: '123 Main St',
description: 'The physical location of the hospital',
})
@Column
location: string;
@ApiProperty({
example: [],
description: 'List of departments in the hospital',
})
@HasMany(() => Department)
departments: Department[];
@ApiProperty({ example: [], description: 'List of workers in the hospital' })
@HasMany(() => User)
workers: User[];
@ApiProperty({
example: [],
description: 'List of drugs available in the hospital',
})
@HasMany(() => Drug)
drugs: Drug[];
// @HasMany(() => Order)
// orders: Order[];
// @HasMany(() => StockAdjustment)
// stockAdjustments: StockAdjustment[];
// @HasMany(() => StockRequest)
// stockRequests: StockRequest[];
// @HasMany(() => Sale)
// sales: Sale[];
// @HasMany(() => Setting)
// settings: Setting[];
}
@Table({
tableName: 'departments',
underscored: true,
})
export class Department extends BaseModel {
@ApiProperty({
example: 'Department A',
description: 'The name of the department',
})
@IsString()
@IsNotEmpty()
@Column
name: string;
@ApiProperty({
example: 1,
description: 'The ID of the hospital to which the department belongs',
})
@IsUUID()
@IsNotEmpty()
@ForeignKey(() => Facility)
@Column
facilityId: number;
@ApiProperty({
example: {},
description: 'The hospital to which the department belongs',
})
@BelongsTo(() => Facility)
hospital: Facility;
@ApiProperty({
example: [],
description: 'List of workers in the department',
})
@HasMany(() => User)
workers: User[];
@ApiProperty({
example: [],
description: 'List of drugs available in the department',
})
@HasMany(() => Drug)
drugs: Drug[];
// @HasMany(() => Order)
// orders: Order[];
// @HasMany(() => StockAdjustment)
// stockAdjustments: StockAdjustment[];
// @HasMany(() => StockRequest)
// stockRequests: StockRequest[];
// @HasMany(() => Sale)
// sales: Sale[];
// @HasMany(() => Setting)
// settings: Setting[];
}
import {
BelongsTo,
Column,
DataType,
DeletedAt,
ForeignKey,
Table,
} from 'sequelize-typescript';
import { BaseModel } from '../../shared/models/base.model';
import { Department, Facility } from 'src/inventory/models/inventory.model';
@Table({
tableName: 'users',
underscored: true,
})
export class User extends BaseModel {
@Column
fullName: string;
@Column({ unique: true })
email: string;
@Column
phoneNumber: string;
@ForeignKey(() => Facility)
@Column
facilityId: string;
@BelongsTo(() => Facility)
facility: Facility;
@ForeignKey(() => Department)
@Column({ allowNull: true })
departmentId: string;
@BelongsTo(() => Department)
department?: Department;
@Column
role: string;
@Column
password: string;
@Column({ defaultValue: false })
accountApproved: boolean;
@Column({ defaultValue: 'ACTIVE' })
status: string; //ACTIVE | DEACTIVATED
@Column({ allowNull: true })
deactivatedBy: string;
@DeletedAt
@Column({ type: DataType.DATE, field: 'deleted_at' })
deletedAt: Date;
@Column({ type: DataType.STRING, field: 'deleted_by' })
deletedBy: string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment