Skip to content

Commit

Permalink
feat(core/ui): refresh auto computed fields (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmyb authored Dec 20, 2024
1 parent eab60f2 commit f569c5e
Show file tree
Hide file tree
Showing 37 changed files with 279 additions and 26 deletions.
1 change: 1 addition & 0 deletions apps/core/src/__tests__/mocks/actionsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const mockActionBase: IActionsListFunction = {
id: 'myAction',
name: 'MyAction',
description: 'My action',
compute: false,
input_types: Object.values(ActionsListIOTypes),
output_types: Object.values(ActionsListIOTypes),
action: jest.fn()
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/_types/actionsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface IActionsListFunction<
output_types: ActionsListIOTypes[];
params?: Array<ActionsListParamsConfig<ParamsRequired>>;
error_message?: ISystemTranslation;
compute: boolean;
action: (
values: IValue[],
params: PartialByCondition<ActionsListParams<keyof ParamsRequired>, ParamsRequired>,
Expand Down
6 changes: 4 additions & 2 deletions apps/core/src/app/core/attributeApp/attributeApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default function (deps: IDeps): ICoreAttributeApp {
coreApp.filterSysTranslationField(attributeData.description, args.lang || []),
input_types: (attributeData, _, ctx) => attributeDomain.getInputTypes({attrData: attributeData, ctx}),
output_types: (attributeData, _, ctx) => attributeDomain.getOutputTypes({attrData: attributeData, ctx}),
compute: (attributeData, _, ctx) => attributeDomain.doesCompute(attributeData),
metadata_fields: async (attributeData: IAttribute, _, ctx) =>
!!attributeData.metadata_fields
? Promise.all(
Expand Down Expand Up @@ -138,6 +139,7 @@ export default function (deps: IDeps): ICoreAttributeApp {
output_types: ActionListIOTypes!,
metadata_fields: [StandardAttribute!],
libraries: [Library!],
compute: Boolean!,
# Permissions for this attribute.
# If record is specified, returns permissions for this specific record, otherwise returns global attribute permissions
Expand Down Expand Up @@ -195,14 +197,14 @@ export default function (deps: IDeps): ICoreAttributeApp {
maxLength: Int
}
type LinkAttribute implements Attribute{
type LinkAttribute implements Attribute {
${attributesInterfaceSchema}
linked_library: Library,
values_list: LinkValuesListConf,
reverse_link: String
}
type TreeAttribute implements Attribute{
type TreeAttribute implements Attribute {
${attributesInterfaceSchema}
linked_tree: Tree,
values_list: TreeValuesListConf
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/dateRangeToNumberAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function (): IActionsListFunction {
description: 'Convert date range dates to numbers',
input_types: [ActionsListIOTypes.OBJECT],
output_types: [ActionsListIOTypes.OBJECT],
compute: false,
action: values => ({
values: values.map(valueElement => {
const dateRangeValue = valueElement.payload as IDateRangeValue<string>;
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/encryptAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function (): IActionsListFunction {
description: 'Encrypt value',
input_types: [ActionsListIOTypes.STRING],
output_types: [ActionsListIOTypes.STRING],
compute: false,
action: async values =>
values.reduce(
async (promAcc, valueElement) => {
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/excelCalculationAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default function ({
description: 'Performs an excel calculation',
input_types: [ActionsListIOTypes.STRING, ActionsListIOTypes.NUMBER],
output_types: [ActionsListIOTypes.STRING],
compute: true,
params: [
{
name: 'Description',
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/formatDateAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function (): IActionsListFunction<{localized: false; universal: f
description: 'Convert timestamp to a date',
input_types: [ActionsListIOTypes.NUMBER],
output_types: [ActionsListIOTypes.STRING],
compute: false,
params: [
{
name: 'localized',
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/formatDateRangeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default function (): IActionsListFunction<{localized: false; universal: f
description: 'Convert range timestamps to a range dates',
input_types: [ActionsListIOTypes.OBJECT],
output_types: [ActionsListIOTypes.OBJECT],
compute: false,
params: [
{
name: 'localized',
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/formatNumberAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default function (): IActionsListFunction<{
description: 'Format a number',
input_types: [ActionsListIOTypes.NUMBER],
output_types: [ActionsListIOTypes.STRING],
compute: false,
params: [
{
name: 'decimals',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default function ({
ActionsListIOTypes.OBJECT,
ActionsListIOTypes.BOOLEAN
],
compute: true,
params: [
{
name: 'Description',
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/maskValueAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function (): IActionsListFunction {
description: 'Mask any value by replacing with dots or empty string if no value',
input_types: [ActionsListIOTypes.STRING, ActionsListIOTypes.NUMBER, ActionsListIOTypes.OBJECT],
output_types: [ActionsListIOTypes.STRING],
compute: false,
action: values => {
const _isValueDefined = value =>
value !== null && value !== '' && (typeof value !== 'object' || Object.keys(value).length);
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/parseJSONAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function (): IActionsListFunction {
description: 'Parse a JSON string',
input_types: [ActionsListIOTypes.STRING],
output_types: [ActionsListIOTypes.OBJECT],
compute: false,
action: values => {
const computedValues = values.map(elementValue => {
elementValue.payload = JSON.parse('' + elementValue.payload);
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/toBooleanAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function (): IActionsListFunction {
description: 'Convert value to boolean',
input_types: [ActionsListIOTypes.STRING, ActionsListIOTypes.NUMBER, ActionsListIOTypes.BOOLEAN],
output_types: [ActionsListIOTypes.BOOLEAN],
compute: false,
action: values => {
const computedValues = values.map(elementValue => {
switch (elementValue.payload) {
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/toJSONAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function (): IActionsListFunction {
description: 'Convert value to a JSON string',
input_types: [ActionsListIOTypes.OBJECT],
output_types: [ActionsListIOTypes.STRING],
compute: false,
action: values => {
const computedValues = values.map(elementValue => {
elementValue.payload = elementValue?.payload === null ? null : JSON.stringify(elementValue.payload);
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/toNumberAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function (): IActionsListFunction {
description: 'Convert value to number',
input_types: [ActionsListIOTypes.STRING, ActionsListIOTypes.NUMBER, ActionsListIOTypes.BOOLEAN],
output_types: [ActionsListIOTypes.NUMBER],
compute: false,
action: values => {
const computedValues = values.map(elementValue => {
elementValue.payload = elementValue.payload !== null ? Number(elementValue.payload) : null;
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/toStringAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function (): IActionsListFunction {
description: 'Convert value to string',
input_types: [ActionsListIOTypes.STRING, ActionsListIOTypes.NUMBER, ActionsListIOTypes.BOOLEAN],
output_types: [ActionsListIOTypes.STRING],
compute: false,
action: values => {
const computedValues = values.map(elementValue => {
elementValue.payload = elementValue.payload !== null ? String(elementValue.payload) : null;
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/toUppercaseAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function (): IActionsListFunction {
description: 'Convert the string to uppercase',
input_types: [ActionsListIOTypes.STRING],
output_types: [ActionsListIOTypes.STRING],
compute: false,
action: values => {
const computedValues = values.map(elementValue => {
elementValue.payload =
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/validateEmailAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function (): IActionsListFunction {
description: 'Check if value is a string matching email format',
input_types: [ActionsListIOTypes.STRING],
output_types: [ActionsListIOTypes.STRING],
compute: false,
action: values => {
const allErrors = values.reduce((errors, elementValue) => {
if (!elementValue.payload.match(EMAIL_REGEX)) {
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/validateFormatAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function (): IActionsListFunction {
id: 'validateFormat',
name: 'Validate Format',
description: 'Check if value matches attribute format',
compute: false,
input_types: [
ActionsListIOTypes.STRING,
ActionsListIOTypes.NUMBER,
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/validateRegexAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function (): IActionsListFunction<{regex: true}> {
input_types: [ActionsListIOTypes.STRING],
output_types: [ActionsListIOTypes.STRING],
params: [{name: 'regex', type: 'string', description: 'Validation regex', required: true, default_value: ''}],
compute: false,
action: (values, params) => {
const allErrors = values.reduce<Array<{errorType: Errors; attributeValue: IValue}>>(
(errors, elementValue) => {
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/domain/actions/validateURLAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function (): IActionsListFunction {
description: 'Check if value is a string matching URL format',
input_types: [ActionsListIOTypes.STRING],
output_types: [ActionsListIOTypes.STRING],
compute: false,
action: (values: IValue[]) => {
const allErrors = values.reduce<Array<{errorType: Errors; attributeValue: IValue}>>(
(errors, elementValue) => {
Expand Down
10 changes: 10 additions & 0 deletions apps/core/src/domain/attribute/attributeDomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {IActionsListDomain} from '../actionsList/actionsListDomain';
import getPermissionCachePatternKey from '../permission/helpers/getPermissionCachePatternKey';
import {getActionsListToSave, getAllowedInputTypes, getAllowedOutputTypes} from './helpers/attributeALHelper';
import {validateAttributeData} from './helpers/attributeValidationHelper';
import {ActionsListEvents} from '../../_types/actionsList';

export interface IAttributeDomain {
getAttributeProperties({id, ctx}: {id: string; ctx: IQueryInfos}): Promise<IAttribute>;
Expand Down Expand Up @@ -55,6 +56,8 @@ export interface IAttributeDomain {
* Retrieve libraries linked to attribute
*/
getAttributeLibraries(params: {attributeId: string; ctx: IQueryInfos}): Promise<ILibrary[]>;

doesCompute(attrData: IAttribute): boolean;
}

export interface IAttributeDomainDeps {
Expand Down Expand Up @@ -112,6 +115,13 @@ export default function ({
};

return {
doesCompute(attrData): boolean {
const availableActions = actionsListDomain.getAvailableActions();

return attrData.actions_list[ActionsListEvents.GET_VALUE].some(
action => availableActions.find(availableAction => availableAction.id === action.id).compute
);
},
async getLibraryAttributes(libraryId: string, ctx): Promise<IAttribute[]> {
const _execute = async () => {
const libs = await libraryRepo.getLibraries({params: {filters: {id: libraryId}}, ctx});
Expand Down
10 changes: 9 additions & 1 deletion libs/ui/src/__mocks__/common/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@ export const mockFormAttribute: RecordFormAttributeFragment = {
}
}
],
versions_conf: null
versions_conf: null,
compute: false
};

export const mockFormAttributeCompute: RecordFormAttributeFragment = {
...mockFormAttribute,
id: 'test_compute_attribute',
format: AttributeFormat.text,
compute: true
};

export const mockFormAttributeTree: RecordFormAttributeTreeAttributeFragment = {
Expand Down
3 changes: 2 additions & 1 deletion libs/ui/src/__mocks__/common/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const mockRecordPropertyWithAttribute: IRecordPropertyWithAttribute = {
},
multiple_values: false,
readonly: false,
permissions: {access_attribute: true, edit_value: true}
permissions: {access_attribute: true, edit_value: true},
compute: false
}
};
Loading

0 comments on commit f569c5e

Please sign in to comment.