88 lines
3.0 KiB
TypeScript
88 lines
3.0 KiB
TypeScript
import { BusinessRecord } from './classes.businessrecord.js';
|
|
import { HandelsRegister } from './classes.handelsregister.js';
|
|
import { JsonlDataProcessor, type SeedEntryType } from './classes.jsonldata.js';
|
|
import * as paths from './paths.js';
|
|
import * as plugins from './plugins.js';
|
|
|
|
export interface IOpenDataConfig {
|
|
downloadDir: string;
|
|
germanBusinessDataDir: string;
|
|
nogitDir: string;
|
|
}
|
|
|
|
export class OpenData {
|
|
public db: plugins.smartdata.SmartdataDb;
|
|
private serviceQenv: plugins.qenv.Qenv;
|
|
private config: IOpenDataConfig;
|
|
|
|
public jsonLDataProcessor: JsonlDataProcessor<SeedEntryType>;
|
|
public handelsregister: HandelsRegister;
|
|
|
|
public CBusinessRecord = plugins.smartdata.setDefaultManagerForDoc(this, BusinessRecord);
|
|
|
|
constructor(configArg: IOpenDataConfig) {
|
|
if (!configArg) {
|
|
throw new Error('@fin.cx/opendata: Configuration is required. You must provide downloadDir, germanBusinessDataDir, and nogitDir paths.');
|
|
}
|
|
if (!configArg.downloadDir || !configArg.germanBusinessDataDir || !configArg.nogitDir) {
|
|
throw new Error('@fin.cx/opendata: All directory paths are required (downloadDir, germanBusinessDataDir, nogitDir).');
|
|
}
|
|
this.config = configArg;
|
|
this.serviceQenv = new plugins.qenv.Qenv(paths.packageDir, this.config.nogitDir);
|
|
}
|
|
|
|
public async start() {
|
|
// Ensure configured directories exist
|
|
await plugins.smartfile.fs.ensureDir(this.config.nogitDir);
|
|
await plugins.smartfile.fs.ensureDir(this.config.downloadDir);
|
|
await plugins.smartfile.fs.ensureDir(this.config.germanBusinessDataDir);
|
|
|
|
this.db = new plugins.smartdata.SmartdataDb({
|
|
mongoDbUrl: await this.serviceQenv.getEnvVarOnDemand('MONGODB_URL'),
|
|
mongoDbName: await this.serviceQenv.getEnvVarOnDemand('MONGODB_NAME'),
|
|
mongoDbUser: await this.serviceQenv.getEnvVarOnDemand('MONGODB_USER'),
|
|
mongoDbPass: await this.serviceQenv.getEnvVarOnDemand('MONGODB_PASS'),
|
|
});
|
|
await this.db.init();
|
|
this.jsonLDataProcessor = new JsonlDataProcessor(
|
|
this.config.germanBusinessDataDir,
|
|
async (entryArg) => {
|
|
const businessRecord = new this.CBusinessRecord();
|
|
businessRecord.id = await this.CBusinessRecord.getNewId();
|
|
businessRecord.data.name = entryArg.name;
|
|
businessRecord.data.germanParsedRegistration = {
|
|
court: entryArg.all_attributes.registered_office,
|
|
number: entryArg.all_attributes._registerNummer,
|
|
type: entryArg.all_attributes._registerArt as 'HRA' | 'HRB',
|
|
};
|
|
await businessRecord.save();
|
|
}
|
|
);
|
|
this.handelsregister = new HandelsRegister(this, this.config.downloadDir);
|
|
await this.handelsregister.start();
|
|
}
|
|
|
|
public async buildInitialDb() {
|
|
await this.jsonLDataProcessor.processDataFromUrl();
|
|
}
|
|
|
|
public async slowValidateDb() {
|
|
|
|
}
|
|
|
|
public async validateSearchByName() {
|
|
|
|
}
|
|
|
|
public async searchDbByBusinessNameAndPostalCode(businessNameArg: string, postalCodeArg: string) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public async stop() {
|
|
await this.db.close();
|
|
await this.handelsregister.stop();
|
|
}
|
|
}
|