remedify-payments-be/src/app.service.ts
2025-02-24 12:46:49 +05:30

77 lines
2.2 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { CommonService } from './common/common.service';
import * as fs from 'fs';
import * as path from 'path';
import { Sequelize } from 'sequelize-typescript';
import { AppConfigService } from './app-config/app-config.service';
import { Utility } from './common/Utility';
@Injectable()
export class AppService {
modelFilePaths: string[] = [] as string[];
constructor(private commonService: CommonService, private configService: AppConfigService) { }
getHello(): string {
return 'Hello World!';
}
initializeSequelize() {
this.getModels();
const dbConfig = this.configService.getDbConfig();
if (this.modelFilePaths.length > 0) {
this.commonService.sequelize = new Sequelize({
database: dbConfig.database,
dialect: 'postgres',
username: dbConfig.user,
password: dbConfig.password,
models: this.modelFilePaths,
modelMatch: (filename, member) => {
return filename.substring(0, filename.indexOf('.entity')) === member.toLowerCase();
},
});
Utility.sequelize = this.commonService.sequelize;
}
const fileConfig = this.configService.initializeFileSystem();
this.commonService.fileConfig = fileConfig;
Utility.fileConfig = fileConfig;
const emailConfig = this.configService.getMailConfig();
this.commonService.mailConfig = emailConfig;
Utility.mailConfig = emailConfig;
}
getModels() {
this.fromDir(__dirname, '.entity', ['.js', '.ts']);
if (this.modelFilePaths.length > 0) {
}
}
fromDir(startPath, filter, extensions) {
if (!fs.existsSync(startPath) || !extensions || extensions.length === 0) {
console.log("no dir ", startPath);
return;
}
const files = fs.readdirSync(startPath);
for (let i = 0; i < files.length; i++) {
const filename = path.join(startPath, files[i]);
const stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
this.fromDir(filename, filter, extensions); //recurse
} else if (filename.includes(filter)) {
extensions.map((extension) => {
if (filename.endsWith(`${filter}${extension}`)) {
this.modelFilePaths.push(filename);
}
})
};
};
}
}