src/model/actionlogs.ts: added actions e.g. update, delete

pull/14/head
leonnicolas 4 years ago
parent 97ee273ae4
commit 660e775de2
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -1,7 +1,7 @@
import { Connection, EntityManager, ObjectType } from 'typeorm'; import { Connection, EntityManager, ObjectType } from 'typeorm';
import { Lockable } from '../../model/CargoBike'; import { Lockable } from '../../model/CargoBike';
import { GraphQLError } from 'graphql'; import { GraphQLError } from 'graphql';
import { ActionLog } from '../../model/ActionLog'; import { ActionLog, Actions } from '../../model/ActionLog';
export function genDateRange (struct: any) { export function genDateRange (struct: any) {
if (struct.to === undefined) { if (struct.to === undefined) {
@ -170,7 +170,7 @@ export class ActionLogger {
return ret; return ret;
} }
static async log (em: EntityManager, target: ObjectType<any>, alias: string, updates: any, userId: number) { static async log (em: EntityManager, target: ObjectType<any>, alias: string, updates: any, userId: number, action: Actions = Actions.UPDATE) {
const oldValues = await em.getRepository(target).createQueryBuilder(alias) const oldValues = await em.getRepository(target).createQueryBuilder(alias)
.select(this.buildSelect(updates, alias)) .select(this.buildSelect(updates, alias))
.where('id = :id', { id: updates.id }) .where('id = :id', { id: updates.id })
@ -201,6 +201,7 @@ export class ActionLogger {
const logEntry : ActionLog = { const logEntry : ActionLog = {
userId: userId, userId: userId,
entity: target.name, entity: target.name,
action: action,
entriesOld: JSON.stringify(oldValues), entriesOld: JSON.stringify(oldValues),
entriesNew: JSON.stringify(updates) entriesNew: JSON.stringify(updates)
}; };

@ -1,5 +1,13 @@
/* eslint no-unused-vars: "off" */
import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn } from 'typeorm'; import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn } from 'typeorm';
export enum Actions {
UPDATE='UPDATE',
SOFT_DELETE='SOFT_DELETE',
DELETE='DELETE',
RESTORE='RESTORE',
}
@Entity() @Entity()
export class ActionLog { export class ActionLog {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
@ -8,6 +16,9 @@ export class ActionLog {
@CreateDateColumn() @CreateDateColumn()
date?: Date; date?: Date;
@Column()
action: string;
@Column() @Column()
userId: number; userId: number;

@ -869,6 +869,7 @@ type ActionLog {
id: ID! id: ID!
userId: ID! userId: ID!
date: Date! date: Date!
action: String!
entity: String! entity: String!
"in json format" "in json format"
entriesOld: String! entriesOld: String!

Loading…
Cancel
Save