Add support for returning transforming activity logs on the front-end
This commit is contained in:
parent
e15985ea39
commit
a5521ecb79
9 changed files with 162 additions and 6 deletions
26
resources/scripts/api/definitions/helpers.ts
Normal file
26
resources/scripts/api/definitions/helpers.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
import { FractalResponseData, FractalResponseList } from '@/api/http';
|
||||
|
||||
type Transformer<T> = (callback: FractalResponseData) => T;
|
||||
|
||||
const isList = (data: FractalResponseList | FractalResponseData): data is FractalResponseList => data.object === 'list';
|
||||
|
||||
function transform<T, M>(data: null | undefined, transformer: Transformer<T>, missing?: M): M;
|
||||
function transform<T, M>(data: FractalResponseData | null | undefined, transformer: Transformer<T>, missing?: M): T | M;
|
||||
function transform<T, M>(data: FractalResponseList | null | undefined, transformer: Transformer<T>, missing?: M): T[] | M;
|
||||
function transform<T> (data: FractalResponseData | FractalResponseList | null | undefined, transformer: Transformer<T>, missing = undefined) {
|
||||
if (data === undefined || data === null) {
|
||||
return missing;
|
||||
}
|
||||
|
||||
if (isList(data)) {
|
||||
return data.data.map(transformer);
|
||||
}
|
||||
|
||||
if (!data || !data.attributes || data.object === 'null_resource') {
|
||||
return missing;
|
||||
}
|
||||
|
||||
return transformer(data);
|
||||
}
|
||||
|
||||
export { transform };
|
3
resources/scripts/api/definitions/index.d.ts
vendored
3
resources/scripts/api/definitions/index.d.ts
vendored
|
@ -1,4 +1,5 @@
|
|||
import { MarkRequired } from 'ts-essentials';
|
||||
import { FractalResponseData, FractalResponseList } from '../http';
|
||||
|
||||
export type UUID = string;
|
||||
|
||||
|
@ -6,7 +7,7 @@ export type UUID = string;
|
|||
export interface Model {}
|
||||
|
||||
interface ModelWithRelationships extends Model {
|
||||
relationships: Record<string, unknown>;
|
||||
relationships: Record<string, FractalResponseData | FractalResponseList | undefined>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,4 +1,16 @@
|
|||
import { Model } from '@/api/definitions';
|
||||
import { Model, UUID } from '@/api/definitions';
|
||||
import { SubuserPermission } from '@/state/server/subusers';
|
||||
|
||||
interface User extends Model {
|
||||
uuid: string;
|
||||
username: string;
|
||||
email: string;
|
||||
image: string;
|
||||
twoFactorEnabled: boolean;
|
||||
createdAt: Date;
|
||||
permissions: SubuserPermission[];
|
||||
can (permission: SubuserPermission): boolean;
|
||||
}
|
||||
|
||||
interface SSHKey extends Model {
|
||||
name: string;
|
||||
|
@ -6,3 +18,15 @@ interface SSHKey extends Model {
|
|||
fingerprint: string;
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
interface ActivityLog extends Model<'actor'> {
|
||||
batch: UUID | null;
|
||||
event: string;
|
||||
ip: string;
|
||||
description: string | null;
|
||||
properties: Record<string, string | unknown>;
|
||||
timestamp: Date;
|
||||
relationships: {
|
||||
actor: User | null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import { SSHKey } from '@definitions/user/models';
|
||||
import * as Models from '@definitions/user/models';
|
||||
import { FractalResponseData } from '@/api/http';
|
||||
import { transform } from '@definitions/helpers';
|
||||
|
||||
export default class Transformers {
|
||||
static toSSHKey (data: Record<any, any>): SSHKey {
|
||||
static toSSHKey (data: Record<any, any>): Models.SSHKey {
|
||||
return {
|
||||
name: data.name,
|
||||
publicKey: data.public_key,
|
||||
|
@ -9,6 +11,37 @@ export default class Transformers {
|
|||
createdAt: new Date(data.created_at),
|
||||
};
|
||||
}
|
||||
|
||||
static toUser ({ attributes }: FractalResponseData): Models.User {
|
||||
return {
|
||||
uuid: attributes.uuid,
|
||||
username: attributes.username,
|
||||
email: attributes.email,
|
||||
image: attributes.image,
|
||||
twoFactorEnabled: attributes['2fa_enabled'],
|
||||
permissions: attributes.permissions || [],
|
||||
createdAt: new Date(attributes.created_at),
|
||||
can (permission): boolean {
|
||||
return this.permissions.includes(permission);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
static toActivityLog ({ attributes }: FractalResponseData): Models.ActivityLog {
|
||||
const { actor } = attributes.relationships || {};
|
||||
|
||||
return {
|
||||
batch: attributes.batch,
|
||||
event: attributes.event,
|
||||
ip: attributes.ip,
|
||||
description: attributes.description,
|
||||
properties: attributes.properties,
|
||||
timestamp: new Date(attributes.timestamp),
|
||||
relationships: {
|
||||
actor: transform(actor as FractalResponseData, this.toUser, null),
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export class MetaTransformers {
|
||||
|
|
|
@ -68,7 +68,7 @@ export interface FractalResponseData {
|
|||
object: string;
|
||||
attributes: {
|
||||
[k: string]: any;
|
||||
relationships?: Record<string, FractalResponseData | FractalResponseList>;
|
||||
relationships?: Record<string, FractalResponseData | FractalResponseList | null | undefined>;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue