Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 1x 1x 1x 1x 1x 1x 1x 1x 17x | import { bind, BindingScope, inject } from '@loopback/core';
import { repository } from '@loopback/repository';
import { SecurityBindings } from '@loopback/security';
import { UserActivityRepository } from '@logchain/repositories';
import { StringUtils } from '@logchain/utils';
import type { UserProfile } from '@logchain/components/aws';
import type { ActivityInfo, StringFormat } from '@logchain/types';
import { Constants } from '@logchain/configs';
@bind({ scope: BindingScope.TRANSIENT })
export class AuditLogService {
@inject(SecurityBindings.USER, { optional: true })
public currentUser?: UserProfile;
constructor(
@repository(UserActivityRepository)
public userActivityRepo: UserActivityRepository,
) { }
/**
* Write activity log based on action.
* @param action The action
* @param model_class The model class
* @param [entity_id]
* @param [data]
* @returns activity
*/
async writeActivity(data: ActivityInfo): Promise<void> {
const user_id = this.currentUser?.id;
const { action, model_class, entity_id, template, resource_name } = data;
const table_name = model_class.definition.settings['postgresql'].table;
if (user_id) {
const desc = this.buildDescription(action, resource_name, template);
try {
await this.userActivityRepo.create({
user_id,
table_name: table_name,
feature_id: entity_id?.toString(),
action: StringUtils.capitalizeFirstLetter(action),
description: desc,
});
} catch (error) {
console.log('[writeActivity] ERROR:', error);
}
}
}
/**
* Build the description.
* @param action The action
* @param model_name The model name
* @param info The info
* @param template The template message. Default: `action model_name`
*/
private buildDescription(action: string, resource_name: string, template: StringFormat,) {
action = StringUtils.capitalizeFirstLetter(action);
if (Constants.ModelsUsingCustomLog.includes(resource_name)) {
return StringUtils.format(template.str, ...template.args);
} else {
const tmp: string[] = resource_name.match(/[A-Z][a-z]+/g) ?? [];
return StringUtils.format(
'{0}d {1} ' + template.str + '.',
action,
tmp.join(' ').toLowerCase(),
...template.args,
);
}
}
}
|