Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechGrancow committed Oct 19, 2023
1 parent 264d8ff commit ca59c48
Show file tree
Hide file tree
Showing 16 changed files with 444 additions and 1 deletion.
49 changes: 49 additions & 0 deletions apps/server/src/apps/deletion.app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* istanbul ignore file */
/* eslint-disable no-console */
import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import express from 'express';

// register source-map-support for debugging
import { install as sourceMapInstall } from 'source-map-support';

// application imports
import { LegacyLogger } from '@src/core/logger';
import { DeletionModule } from '@src/modules/deletion';
import { enableOpenApiDocs } from '@src/shared/controller/swagger';

async function bootstrap() {
sourceMapInstall();

// create the NestJS application on a seperate express instance
const nestExpress = express();

const nestExpressAdapter = new ExpressAdapter(nestExpress);
const nestApp = await NestFactory.create(DeletionModule, nestExpressAdapter);
// WinstonLogger
nestApp.useLogger(await nestApp.resolve(LegacyLogger));

// customize nest app settings
nestApp.enableCors({ exposedHeaders: ['Content-Disposition'] });
enableOpenApiDocs(nestApp, 'docs');

await nestApp.init();

// mount instances
const rootExpress = express();

const port = 4450;
const basePath = '/api/v3';

// exposed alias mounts
rootExpress.use(basePath, nestExpress);
rootExpress.listen(port);

console.log('##########################################');
console.log(`### Start KNL Deletion Server ###`);
console.log(`### Port: ${port} ###`);
console.log(`### Base path: ${basePath} ###`);
console.log('##########################################');
}

void bootstrap();
3 changes: 2 additions & 1 deletion apps/server/src/config/database.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ interface GlobalConstants {
DB_URL: string;
DB_PASSWORD?: string;
DB_USERNAME?: string;
DEL_DB_URL: string;
}

const usedGlobals: GlobalConstants = globals;

/** Database URL */
export const { DB_URL, DB_PASSWORD, DB_USERNAME } = usedGlobals;
export const { DB_URL, DB_PASSWORD, DB_USERNAME, DEL_DB_URL } = usedGlobals;
37 changes: 37 additions & 0 deletions apps/server/src/modules/deletion/deletion.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Module, NotFoundException } from '@nestjs/common';
import { DB_PASSWORD, DB_USERNAME, DEL_DB_URL } from '@src/config';
import { CoreModule } from '@src/core';
import { Logger } from '@src/core/logger';
import { MikroOrmModule, MikroOrmModuleSyncOptions } from '@mikro-orm/nestjs';
import { AuthenticationModule } from '@src/modules/authentication/authentication.module';
import { AuthorizationModule } from '@src/modules';
import { RabbitMQWrapperTestModule } from '@shared/infra/rabbitmq';
import { Dictionary, IPrimaryKey } from '@mikro-orm/core';
import { DeletionRequestService } from './services/deletion-request.service';
import { DeletionRequestRepo } from './repo/deletion-request.repo';

const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = {
findOneOrFailHandler: (entityName: string, where: Dictionary | IPrimaryKey) =>
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
new NotFoundException(`The requested ${entityName}: ${where} has not been found.`),
};

@Module({
imports: [
AuthorizationModule,
AuthenticationModule,
CoreModule,
RabbitMQWrapperTestModule,
MikroOrmModule.forRoot({
...defaultMikroOrmOptions,
type: 'mongo',
clientUrl: DEL_DB_URL,
password: DB_PASSWORD,
user: DB_USERNAME,
entities: [],
}),
// ConfigModule.forRoot(createConfigModuleOptions(config)),
],
providers: [Logger, DeletionRequestService, DeletionRequestRepo],
})
export class DeletionModule {}
37 changes: 37 additions & 0 deletions apps/server/src/modules/deletion/domain/deletion-log.do.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { EntityId } from '@shared/domain/types';
import { AuthorizableObject, DomainObject } from '@shared/domain/domain-object';

export interface DeletionLogProps extends AuthorizableObject {
createdAt: Date;
updatedAt: Date;
scope?: string;
operation?: string;
docIds?: EntityId[];
deletionRequestId?: EntityId;
}

export class DeletionLog extends DomainObject<DeletionLogProps> {
get createdAt(): Date {
return this.props.createdAt;
}

get updatedAt(): Date {
return this.props.updatedAt;
}

get scope(): string | undefined {
return this.props.scope;
}

get operation(): string | undefined {
return this.props.operation;
}

get deletionRequestId(): EntityId | undefined {
return this.props.deletionRequestId;
}

get docIds(): EntityId[] | undefined {
return this.props.docIds;
}
}
32 changes: 32 additions & 0 deletions apps/server/src/modules/deletion/domain/deletion-request.do.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { EntityId } from '@shared/domain/types';
import { AuthorizableObject, DomainObject } from '@shared/domain/domain-object';

export interface DeletionRequestProps extends AuthorizableObject {
createdAt?: Date;
updatedAt?: Date;
source?: string;
deleteAfter?: Date;
userId?: EntityId;
}

export class DeletionRequest extends DomainObject<DeletionRequestProps> {
get createdAt(): Date | undefined {
return this.props.createdAt;
}

get updatedAt(): Date | undefined {
return this.props.updatedAt;
}

get source(): string | undefined {
return this.props.source;
}

get deleteAfter(): Date | undefined {
return this.props.deleteAfter;
}

get userId(): EntityId | undefined {
return this.props.userId;
}
}
58 changes: 58 additions & 0 deletions apps/server/src/modules/deletion/entities/deletion-log.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Entity, Property } from '@mikro-orm/core';
import { BaseEntityWithTimestamps, EntityId } from '@shared/domain';
import { ObjectId } from '@mikro-orm/mongodb';

export interface DeletionLogEntityProps {
id: EntityId;
createdAt?: Date;
updatedAt?: Date;
scope?: string;
operation?: string;
docIds?: ObjectId[];
deletionRequestId?: ObjectId;
}

@Entity({ tableName: 'deletionlogs' })
export class DeletionLogEntity extends BaseEntityWithTimestamps {
@Property({ nullable: true })
scope?: string;

@Property({ nullable: true })
operation?: string;

@Property({ nullable: true })
docIds?: ObjectId[];

@Property({ nullable: true })
deletionRequestId?: ObjectId;

constructor(props: DeletionLogEntityProps) {
super();

if (props.id !== undefined) {
this.id = props.id;
}

if (props.createdAt !== undefined) {
this.createdAt = props.createdAt;
}

if (props.scope !== undefined) {
this.scope = props.scope;
}

if (props.operation !== undefined) {
this.operation = props.operation;
}

if (props.updatedAt !== undefined) {
this.updatedAt = props.updatedAt;
}

if (props.deletionRequestId !== undefined) {
this.deletionRequestId = props.deletionRequestId;
}

this.docIds = props.docIds;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Entity, Property } from '@mikro-orm/core';
import { BaseEntityWithTimestamps, EntityId } from '@shared/domain';
import { ObjectId } from '@mikro-orm/mongodb';

export interface DeletionRequestEntityProps {
id?: EntityId;
source?: string;
deleteAfter?: Date;
userId?: ObjectId;
}

@Entity({ tableName: 'deletionrequests' })
export class DeletionRequestEntity extends BaseEntityWithTimestamps {
@Property({ nullable: true })
deleteAfter?: Date;

@Property({ fieldName: 'userToDeletion', nullable: true })
userId?: ObjectId;

@Property({ nullable: true })
source?: string;

constructor(props: DeletionRequestEntityProps) {
super();

if (props.source !== undefined) {
this.source = props.source;
}

if (props.deleteAfter !== undefined) {
this.deleteAfter = props.deleteAfter;
}

if (props.userId !== undefined) {
this.userId = props.userId;
}
}
}
1 change: 1 addition & 0 deletions apps/server/src/modules/deletion/entities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './deletion-request.entity';

Check failure on line 1 in apps/server/src/modules/deletion/entities/index.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Insert `⏎`
1 change: 1 addition & 0 deletions apps/server/src/modules/deletion/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './deletion.module';
25 changes: 25 additions & 0 deletions apps/server/src/modules/deletion/repo/deletion-log.repo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { EntityManager } from '@mikro-orm/mongodb';
import { Injectable } from '@nestjs/common';
import { EntityId } from '@shared/domain';
import { DeletionLog } from '../domain/deletion-log.do';
import { DeletionLogEntity } from '../entities/deletion-log.entity';
import { DeletionLogMapper } from './mapper/deletion-log.mapper';

@Injectable()
export class DeletionLogRepo {
constructor(private readonly em: EntityManager) {}

async findById(id: EntityId): Promise<DeletionLog> {
const deletionRequest: DeletionLogEntity = await this.em.findOneOrFail(DeletionLogEntity, { id });

const mapped: DeletionLog = DeletionLogMapper.mapToDO(deletionRequest);

return mapped;
}

// create

// update

// delete
}
45 changes: 45 additions & 0 deletions apps/server/src/modules/deletion/repo/deletion-request.repo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { EntityManager } from '@mikro-orm/mongodb';
import { Injectable } from '@nestjs/common';
import { EntityId } from '@shared/domain';
import { DeletionRequest } from '../domain/deletion-request.do';
import { DeletionRequestEntity } from '../entities';
import { DeletionRequestMapper } from './mapper/deletion-request.mapper';

@Injectable()
export class DeletionRequestRepo {
constructor(private readonly em: EntityManager) {}

async findById(id: EntityId): Promise<DeletionRequest> {
const deletionRequest: DeletionRequestEntity = await this.em.findOneOrFail(DeletionRequestEntity, { id });

const mapped: DeletionRequest = DeletionRequestMapper.mapToDO(deletionRequest);

return mapped;
}

// create
async create(deletionRequest: DeletionRequest): Promise<void> {
const deletionRequestEntity = DeletionRequestMapper.mapToEntity(deletionRequest);
await this.em.persistAndFlush(deletionRequestEntity);
}

// update
async update(deletionRequest: DeletionRequest): Promise<void> {
const deletionRequestEntity = DeletionRequestMapper.mapToEntity(deletionRequest);
await this.em.persistAndFlush(deletionRequestEntity);
}

// find
async findAllItemsByDeletionDate(): Promise<DeletionRequest[]> {
const currentDate = new Date();
const itemsToDelete: DeletionRequestEntity[] = await this.em.find(DeletionRequestEntity, {
deleteAfter: { $lt: currentDate },
});

const mapped: DeletionRequest[] = itemsToDelete.map((entity) => DeletionRequestMapper.mapToDO(entity));

return mapped;
}

// delete
}
1 change: 1 addition & 0 deletions apps/server/src/modules/deletion/repo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './classes.repo';
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { DeletionLogEntity } from '../../entities/deletion-log.entity';
import { DeletionLog } from '../../domain/deletion-log.do';

export class DeletionLogMapper {
static mapToDO(entity: DeletionLogEntity): DeletionLog {
return new DeletionLog({
id: entity.id,
createdAt: entity.createdAt,
updatedAt: entity.updatedAt,
scope: entity.scope,
operation: entity.operation,
docIds: entity.docIds?.map((docId) => docId.toHexString()),
deletionRequestId: entity.deletionRequestId?.toHexString(),
});
}

static mapToEntity(domainObject: DeletionLog): DeletionLogEntity {
return new DeletionLogEntity({
id: domainObject.id,
createdAt: domainObject.createdAt,
updatedAt: domainObject.updatedAt,
scope: domainObject.scope,
operation: domainObject.operation,
docIds: domainObject.docIds?.map((docId) => new ObjectId(docId)),
deletionRequestId: new ObjectId(domainObject.deletionRequestId),
});
}

// static mapToDOs(entities: DeletionLogEntity[]): DeletionLog[] {
// return entities.map((entity) => this.mapToDO(entity));
// }

// static mapToEntities(domainObjects: DeletionLog[]): DeletionLogEntity[] {
// return domainObjects.map((domainObject) => this.mapToEntity(domainObject));
// }
}
Loading

0 comments on commit ca59c48

Please sign in to comment.