-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f100ea
commit 0800a4b
Showing
8 changed files
with
307 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
api/services/export/src/repositories/ExportRepository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { provider } from '@ilos/common'; | ||
import { PostgresConnection } from '@ilos/connection-postgres'; | ||
import { ExportParams } from '../models/ExportParams'; | ||
|
||
export type Export = { | ||
_id: number; | ||
created_at: Date; | ||
updated_at: Date; | ||
created_by: number; | ||
uuid: string; | ||
status: ExportStatus; | ||
progress: number; | ||
type: ExportType; | ||
download_url: string; | ||
params: ExportParams; | ||
error: any; | ||
stats: any; | ||
}; | ||
export enum ExportStatus { | ||
PENDING = 'pending', | ||
RUNNING = 'running', | ||
SUCCESS = 'success', | ||
FAILURE = 'failure', | ||
} | ||
export enum ExportType { | ||
REGULAR = 'regular', | ||
OPENDATA = 'opendata', | ||
OPERATOR = 'operator', | ||
TERRITORY = 'territory', | ||
REGISTRY = 'registry', | ||
} | ||
export type ExportCreateData = Pick<Export, 'created_by' | 'type' | 'params'>; | ||
export type ExportUpdateData = Partial<Pick<Export, 'status' | 'progress' | 'download_url' | 'error' | 'stats'>>; | ||
|
||
export interface ExportRepositoryInterface { | ||
create(data: ExportCreateData): Promise<number>; | ||
get(id: number): Promise<any>; | ||
update(id: number, data: ExportUpdateData): Promise<void>; | ||
delete(id: number): Promise<void>; | ||
list(): Promise<any[]>; | ||
} | ||
|
||
export abstract class ExportRepositoryInterfaceResolver implements ExportRepositoryInterface { | ||
public async create(data: ExportCreateData): Promise<number> { | ||
throw new Error('Not implemented'); | ||
} | ||
public async get(id: number): Promise<any> { | ||
throw new Error('Not implemented'); | ||
} | ||
public async update(id: number, data: ExportUpdateData): Promise<void> { | ||
throw new Error('Not implemented'); | ||
} | ||
public async delete(id: number): Promise<void> { | ||
throw new Error('Not implemented'); | ||
} | ||
public async list(): Promise<any[]> { | ||
throw new Error('Not implemented'); | ||
} | ||
} | ||
|
||
@provider({ | ||
identifier: ExportRepositoryInterfaceResolver, | ||
}) | ||
export class ExportRepository implements ExportRepositoryInterface { | ||
protected readonly table = 'export.exports'; | ||
|
||
constructor(protected connection: PostgresConnection) {} | ||
|
||
public async create(data: ExportCreateData): Promise<number> { | ||
const { rows } = await this.connection.getClient().query({ | ||
text: `INSERT INTO ${this.table} (created_by, type, params) VALUES ($1, $2, $3) RETURNING _id`, | ||
values: [data.created_by, data.type, data.params], | ||
}); | ||
return rows[0]._id; | ||
} | ||
|
||
public async get(id: number): Promise<any> { | ||
const { rows } = await this.connection.getClient().query({ | ||
text: `SELECT * FROM ${this.table} WHERE _id = $1`, | ||
values: [id], | ||
}); | ||
return rows[0]; | ||
} | ||
|
||
public async update(id: number, data: ExportUpdateData): Promise<void> { | ||
await this.connection.getClient().query({ | ||
text: `UPDATE ${this.table} SET ${Object.keys(data) | ||
.map((key, index) => `${key} = $${index + 2}`) | ||
.join(', ')} WHERE _id = $1`, | ||
values: [id, ...Object.values(data)], | ||
}); | ||
} | ||
|
||
public async delete(id: number): Promise<void> { | ||
await this.connection.getClient().query({ | ||
text: `DELETE FROM ${this.table} WHERE _id = $1`, | ||
values: [id], | ||
}); | ||
} | ||
|
||
public async list(): Promise<any[]> { | ||
const { rows } = await this.connection.getClient().query({ | ||
text: `SELECT * FROM ${this.table}`, | ||
}); | ||
return rows; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { provider } from '@ilos/common'; | ||
import { PostgresConnection } from '@ilos/connection-postgres'; | ||
|
||
export enum LogStatus { | ||
SUCCESS = 'success', | ||
FAILURE = 'failure', | ||
} | ||
|
||
export interface LogRepositoryInterface { | ||
add(export_id: number, type: LogStatus, message: string): Promise<void>; | ||
} | ||
|
||
export abstract class LogRepositoryInterfaceResolver implements LogRepositoryInterface { | ||
public async add(export_id: number, type: LogStatus, message: string): Promise<void> { | ||
throw new Error('Not implemented'); | ||
} | ||
} | ||
|
||
@provider({ | ||
identifier: LogRepositoryInterfaceResolver, | ||
}) | ||
export class LogRepository implements LogRepositoryInterface { | ||
protected readonly table = 'export.logs'; | ||
|
||
constructor(protected connection: PostgresConnection) {} | ||
|
||
public async add(export_id: number, type: LogStatus, message: string): Promise<void> { | ||
await this.connection.getClient().query({ | ||
text: `INSERT INTO ${this.table} (export_id, type, message) VALUES ($1, $2, $3)`, | ||
values: [export_id, type, message], | ||
}); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
api/services/export/src/repositories/RecipientRepository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { provider } from '@ilos/common'; | ||
import { PostgresConnection } from '@ilos/connection-postgres'; | ||
|
||
export type Recipient = { | ||
_id: number; | ||
scrambled_at: Date; | ||
export_id: number; | ||
email: string; | ||
fullname: string; | ||
message: string; | ||
}; | ||
|
||
export type CreateRecipientData = Pick<Recipient, 'export_id' | 'email' | 'fullname' | 'message'>; | ||
|
||
export interface RecipientRepositoryInterface { | ||
create(data: CreateRecipientData): Promise<number>; | ||
anonymize(export_id: number): Promise<void>; | ||
} | ||
|
||
export abstract class RecipientRepositoryInterfaceResolver implements RecipientRepositoryInterface { | ||
public async create(data: CreateRecipientData): Promise<number> { | ||
throw new Error('Not implemented'); | ||
} | ||
public async anonymize(export_id: number): Promise<void> { | ||
throw new Error('Not implemented'); | ||
} | ||
} | ||
|
||
@provider({ | ||
identifier: RecipientRepositoryInterfaceResolver, | ||
}) | ||
export class RecipientRepository implements RecipientRepositoryInterface { | ||
protected readonly table = 'export.recipients'; | ||
|
||
constructor(protected connection: PostgresConnection) {} | ||
|
||
public async create(data: CreateRecipientData): Promise<number> { | ||
const { rows } = await this.connection.getClient().query({ | ||
text: ` | ||
INSERT INTO ${this.table} | ||
(export_id, email, fullname, message) | ||
VALUES ($1, $2, $3, $4) | ||
RETURNING _id`, | ||
values: [data.export_id, data.email, data.fullname, data.message], | ||
}); | ||
return rows[0]._id; | ||
} | ||
|
||
public async anonymize(export_id: number): Promise<void> { | ||
await this.connection.getClient().query({ | ||
text: ` | ||
UPDATE ${this.table} | ||
SET | ||
scrambled_at = NOW(), | ||
email = NULL, | ||
fullname = NULL, | ||
message = NULL | ||
WHERE export_id = $1`, | ||
values: [export_id], | ||
}); | ||
} | ||
} |
Oops, something went wrong.