From a6e9a20e76a2fc45d4c86e45d714dc31cf3cba61 Mon Sep 17 00:00:00 2001 From: Riku Rouvila Date: Fri, 13 Dec 2024 17:19:26 +0200 Subject: [PATCH 1/3] add conditional rules for action visibility --- src/form/tennis-club-membership.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/form/tennis-club-membership.ts b/src/form/tennis-club-membership.ts index 4b58e5615..69d05aa10 100644 --- a/src/form/tennis-club-membership.ts +++ b/src/form/tennis-club-membership.ts @@ -10,6 +10,7 @@ */ import { defineConfig } from '@opencrvs/toolkit/events' +import { defineConditional } from '@opencrvs/toolkit/conditionals' export const tennisClubMembershipEvent = defineConfig({ id: 'TENNIS_CLUB_MEMBERSHIP', @@ -240,6 +241,31 @@ export const tennisClubMembershipEvent = defineConfig({ 'This is shown as the action name anywhere the user can trigger the action from', id: 'event.tennis-club-membership.action.declare.label' }, + allowedWhen: defineConditional({ + type: 'object', + allOf: [ + { + properties: { + $event: { + properties: { + actions: { + contains: { + type: 'object', + properties: { + type: { + const: 'DECLARE' + } + }, + required: ['type'] + } + } + } + } + } + } + ] + }), + forms: [ { label: { From a1d81ba68ea53c630a0a47fcb6a1b16745173f59 Mon Sep 17 00:00:00 2001 From: Riku Rouvila Date: Sat, 14 Dec 2024 15:15:21 +0200 Subject: [PATCH 2/3] add more actions and conditionality --- src/form/tennis-club-membership.ts | 398 +++++++++++------------------ 1 file changed, 154 insertions(+), 244 deletions(-) diff --git a/src/form/tennis-club-membership.ts b/src/form/tennis-club-membership.ts index 69d05aa10..75d99f7f6 100644 --- a/src/form/tennis-club-membership.ts +++ b/src/form/tennis-club-membership.ts @@ -9,8 +9,121 @@ * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. */ -import { defineConfig } from '@opencrvs/toolkit/events' -import { defineConditional } from '@opencrvs/toolkit/conditionals' +import { defineConfig, defineForm } from '@opencrvs/toolkit/events' +import { + defineConditional, + or, + eventHasAction, + userHasScope, + and, + not +} from '@opencrvs/toolkit/conditionals' + +const TENNIS_CLUB_FORM = defineForm({ + label: { + id: 'event.tennis-club-membership.action.declare.form.label', + defaultMessage: 'Tennis club membership application', + description: 'This is what this form is referred as in the system' + }, + review: { + title: { + id: 'event.tennis-club-membership.action.declare.form.review.title', + defaultMessage: 'Member declaration for {firstname} {surname}', + description: 'Title of the form to show in review page' + } + }, + active: true, + version: { + id: '1.0.0', + label: { + id: 'event.tennis-club-membership.action.declare.form.version.1', + defaultMessage: 'Version 1', + description: 'This is the first version of the form' + } + }, + pages: [ + { + id: 'applicant', + title: { + id: 'event.tennis-club-membership.action.declare.form.section.who.title', + defaultMessage: 'Who is applying for the membership?', + description: 'This is the title of the section' + }, + fields: [ + { + id: 'applicant.firstname', + type: 'TEXT', + required: true, + label: { + defaultMessage: "Applicant's first name", + description: 'This is the label for the field', + id: 'event.tennis-club-membership.action.declare.form.section.who.field.firstname.label' + } + }, + { + id: 'applicant.surname', + type: 'TEXT', + required: true, + label: { + defaultMessage: "Applicant's surname", + description: 'This is the label for the field', + id: 'event.tennis-club-membership.action.declare.form.section.who.field.surname.label' + } + }, + { + id: 'applicant.dob', + type: 'DATE', + required: true, + label: { + defaultMessage: "Applicant's date of birth", + description: 'This is the label for the field', + id: 'event.tennis-club-membership.action.declare.form.section.who.field.dob.label' + } + } + ] + }, + { + id: 'recommender', + title: { + id: 'event.tennis-club-membership.action.declare.form.section.recommender.title', + defaultMessage: 'Who is recommending the applicant?', + description: 'This is the title of the section' + }, + fields: [ + { + id: 'recommender.firstname', + type: 'TEXT', + required: true, + label: { + defaultMessage: "Recommender's first name", + description: 'This is the label for the field', + id: 'event.tennis-club-membership.action.declare.form.section.recommender.field.firstname.label' + } + }, + { + id: 'recommender.surname', + type: 'TEXT', + required: true, + label: { + defaultMessage: "Recommender's surname", + description: 'This is the label for the field', + id: 'event.tennis-club-membership.action.declare.form.section.recommender.field.surname.label' + } + }, + { + id: 'recommender.id', + type: 'TEXT', + required: true, + label: { + defaultMessage: "Recommender's membership ID", + description: 'This is the label for the field', + id: 'event.tennis-club-membership.action.declare.form.section.recommender.field.id.label' + } + } + ] + } + ] +}) export const tennisClubMembershipEvent = defineConfig({ id: 'TENNIS_CLUB_MEMBERSHIP', @@ -120,259 +233,56 @@ export const tennisClubMembershipEvent = defineConfig({ { type: 'DECLARE', label: { - defaultMessage: 'Send an application', + defaultMessage: 'Declare', description: 'This is shown as the action name anywhere the user can trigger the action from', id: 'event.tennis-club-membership.action.declare.label' }, - forms: [ - { - label: { - id: 'event.tennis-club-membership.action.declare.form.label', - defaultMessage: 'Tennis club membership application', - description: 'This is what this form is referred as in the system' - }, - review: { - title: { - id: 'event.tennis-club-membership.action.declare.form.review.title', - defaultMessage: 'Member declaration for {firstname} {surname}', - description: 'Title of the form to show in review page' - } - }, - active: true, - version: { - id: '1.0.0', - label: { - id: 'event.tennis-club-membership.action.declare.form.version.1', - defaultMessage: 'Version 1', - description: 'This is the first version of the form' - } - }, - pages: [ - { - id: 'applicant', - title: { - id: 'event.tennis-club-membership.action.declare.form.section.who.title', - defaultMessage: 'Who is applying for the membership?', - description: 'This is the title of the section' - }, - fields: [ - { - id: 'applicant.firstname', - type: 'TEXT', - required: true, - label: { - defaultMessage: "Applicant's first name", - description: 'This is the label for the field', - id: 'event.tennis-club-membership.action.declare.form.section.who.field.firstname.label' - } - }, - { - id: 'applicant.surname', - type: 'TEXT', - required: true, - label: { - defaultMessage: "Applicant's surname", - description: 'This is the label for the field', - id: 'event.tennis-club-membership.action.declare.form.section.who.field.surname.label' - } - }, - { - id: 'applicant.dob', - type: 'DATE', - required: true, - label: { - defaultMessage: "Applicant's date of birth", - description: 'This is the label for the field', - id: 'event.tennis-club-membership.action.declare.form.section.who.field.dob.label' - } - } - ] - }, - { - id: 'recommender', - title: { - id: 'event.tennis-club-membership.action.declare.form.section.recommender.title', - defaultMessage: 'Who is recommending the applicant?', - description: 'This is the title of the section' - }, - fields: [ - { - id: 'recommender.firstname', - type: 'TEXT', - required: true, - label: { - defaultMessage: "Recommender's first name", - description: 'This is the label for the field', - id: 'event.tennis-club-membership.action.declare.form.section.recommender.field.firstname.label' - } - }, - { - id: 'recommender.surname', - type: 'TEXT', - required: true, - label: { - defaultMessage: "Recommender's surname", - description: 'This is the label for the field', - id: 'event.tennis-club-membership.action.declare.form.section.recommender.field.surname.label' - } - }, - { - id: 'recommender.id', - type: 'TEXT', - required: true, - label: { - defaultMessage: "Recommender's membership ID", - description: 'This is the label for the field', - id: 'event.tennis-club-membership.action.declare.form.section.recommender.field.id.label' - } - } - ] - } - ] - } - ] + forms: [TENNIS_CLUB_FORM], + allowedWhen: defineConditional(not(eventHasAction('DECLARE'))) + }, + { + type: 'VALIDATE', + label: { + defaultMessage: 'Validate', + description: + 'This is shown as the action name anywhere the user can trigger the action from', + id: 'event.tennis-club-membership.action.validate.label' + }, + allowedWhen: defineConditional(eventHasAction('DECLARE')), + forms: [TENNIS_CLUB_FORM] }, { type: 'REGISTER', label: { - defaultMessage: 'Send an application', + defaultMessage: 'Register', description: 'This is shown as the action name anywhere the user can trigger the action from', - id: 'event.tennis-club-membership.action.declare.label' + id: 'event.tennis-club-membership.action.register.label' }, - allowedWhen: defineConditional({ - type: 'object', - allOf: [ - { - properties: { - $event: { - properties: { - actions: { - contains: { - type: 'object', - properties: { - type: { - const: 'DECLARE' - } - }, - required: ['type'] - } - } - } - } - } - } - ] - }), - - forms: [ - { - label: { - id: 'event.tennis-club-membership.action.declare.form.label', - defaultMessage: 'Tennis club membership application', - description: 'This is what this form is referred as in the system' - }, - review: { - title: { - id: 'event.tennis-club-membership.action.declare.form.review.title', - defaultMessage: 'Member registration for {firstname} {surname}', - description: 'Title of the form to show in registration page' - } - }, - active: true, - version: { - id: '1.0.0', - label: { - id: 'event.tennis-club-membership.action.declare.form.version.1', - defaultMessage: 'Version 1', - description: 'This is the first version of the form' - } - }, - pages: [ - { - id: 'applicant', - title: { - id: 'event.tennis-club-membership.action.declare.form.section.who.title', - defaultMessage: 'Who is applying for the membership?', - description: 'This is the title of the section' - }, - fields: [ - { - id: 'applicant.firstname', - type: 'TEXT', - required: true, - label: { - defaultMessage: "Applicant's first name", - description: 'This is the label for the field', - id: 'event.tennis-club-membership.action.declare.form.section.who.field.firstname.label' - } - }, - { - id: 'applicant.surname', - type: 'TEXT', - required: true, - label: { - defaultMessage: "Applicant's surname", - description: 'This is the label for the field', - id: 'event.tennis-club-membership.action.declare.form.section.who.field.surname.label' - } - }, - { - id: 'applicant.dob', - type: 'DATE', - required: true, - label: { - defaultMessage: "Applicant's date of birth", - description: 'This is the label for the field', - id: 'event.tennis-club-membership.action.declare.form.section.who.field.dob.label' - } - } - ] - }, - { - id: 'recommender', - title: { - id: 'event.tennis-club-membership.action.declare.form.section.recommender.title', - defaultMessage: 'Who is recommending the applicant?', - description: 'This is the title of the section' - }, - fields: [ - { - id: 'recommender.firstname', - type: 'TEXT', - required: true, - label: { - defaultMessage: "Recommender's first name", - description: 'This is the label for the field', - id: 'event.tennis-club-membership.action.declare.form.section.recommender.field.firstname.label' - } - }, - { - id: 'recommender.surname', - type: 'TEXT', - required: true, - label: { - defaultMessage: "Recommender's surname", - description: 'This is the label for the field', - id: 'event.tennis-club-membership.action.declare.form.section.recommender.field.surname.label' - } - }, - { - id: 'recommender.id', - type: 'TEXT', - required: true, - label: { - defaultMessage: "Recommender's membership ID", - description: 'This is the label for the field', - id: 'event.tennis-club-membership.action.declare.form.section.recommender.field.id.label' - } - } - ] - } - ] - } - ] + allowedWhen: defineConditional( + or( + eventHasAction('VALIDATE'), + and(eventHasAction('DECLARE'), userHasScope('register')) + ) + ), + forms: [TENNIS_CLUB_FORM] + }, + { + type: 'CUSTOM', + label: { + defaultMessage: 'My custom action', + description: + 'This is shown as the action name anywhere the user can trigger the action from', + id: 'event.tennis-club-membership.action.sdf.label' + }, + allowedWhen: defineConditional( + or( + eventHasAction('VALIDATE'), + and(eventHasAction('DECLARE'), userHasScope('register')) + ) + ), + forms: [] } ] }) From cf4c1d4b7a7ebbb89e5929f38378bbe8d03f8b85 Mon Sep 17 00:00:00 2001 From: Riku Rouvila Date: Sat, 14 Dec 2024 20:29:27 +0200 Subject: [PATCH 3/3] do not show register action after a record already has a register action --- src/form/tennis-club-membership.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/form/tennis-club-membership.ts b/src/form/tennis-club-membership.ts index 75d99f7f6..5bc0ae13c 100644 --- a/src/form/tennis-club-membership.ts +++ b/src/form/tennis-club-membership.ts @@ -261,9 +261,12 @@ export const tennisClubMembershipEvent = defineConfig({ id: 'event.tennis-club-membership.action.register.label' }, allowedWhen: defineConditional( - or( - eventHasAction('VALIDATE'), - and(eventHasAction('DECLARE'), userHasScope('register')) + and( + or( + eventHasAction('VALIDATE'), + and(eventHasAction('DECLARE'), userHasScope('register')) + ), + not(eventHasAction('REGISTER')) ) ), forms: [TENNIS_CLUB_FORM]