Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ON-35780 # Added task based replaceable parameters #64

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- `{TASK_NAME}`, `{TASK_GROUP_NAME}` and `{TASK_GROUP_INSTANCE_LABEL}` to replaceable parameters

## [4.2.0] - 2023-10-23

### Added
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 33 additions & 6 deletions src/replaceCustomValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
GeoscapeTypes,
MiscTypes,
PointTypes,
ScheduledTasksTypes,
SubmissionTypes,
} from '@oneblink/types'
import {
Expand All @@ -21,14 +22,20 @@ export type ReplaceInjectablesFormatters = {
formatCurrency: (value: number) => string
}

export type ReplaceInjectablesOptions = ReplaceInjectablesFormatters & {
export type ReplaceInjectablesBaseOptions = ReplaceInjectablesFormatters & {
submission: SubmissionTypes.S3SubmissionData['submission']
userProfile: MiscTypes.UserProfile | undefined
task: ScheduledTasksTypes.Task | undefined
taskGroup: ScheduledTasksTypes.TaskGroup | undefined
taskGroupInstance: ScheduledTasksTypes.TaskGroupInstance | undefined
}

export type ReplaceInjectablesOptions = ReplaceInjectablesBaseOptions & {
form: FormTypes.Form
submissionId: string
submissionTimestamp: string
externalId: string | undefined
previousApprovalId: string | undefined
submission: SubmissionTypes.S3SubmissionData['submission']
userProfile: MiscTypes.UserProfile | undefined
}

const CUSTOM_VALUES = [
Expand Down Expand Up @@ -78,6 +85,19 @@ const CUSTOM_VALUES = [
value: ({ previousApprovalId }: ReplaceInjectablesOptions) =>
previousApprovalId || '',
},
{
string: '{TASK_NAME}',
value: ({ task }: ReplaceInjectablesOptions) => task?.name || '',
},
{
string: '{TASK_GROUP_NAME}',
value: ({ taskGroup }: ReplaceInjectablesOptions) => taskGroup?.name || '',
},
{
string: '{TASK_GROUP_INSTANCE_LABEL}',
value: ({ taskGroupInstance }: ReplaceInjectablesOptions) =>
taskGroupInstance?.label || '',
},
]

/**
Expand Down Expand Up @@ -306,9 +326,7 @@ export function replaceInjectablesWithElementValues(
text: string,
options: {
formElements: FormTypes.FormElement[]
submission: SubmissionTypes.S3SubmissionData['submission']
userProfile: MiscTypes.UserProfile | undefined
} & ReplaceInjectablesFormatters,
} & ReplaceInjectablesBaseOptions,
): string {
const keys: Array<keyof MiscTypes.UserProfile> = ['email']
// User based values should be replaced with an empty string if
Expand Down Expand Up @@ -411,6 +429,9 @@ export function replaceInjectablesWithSubmissionValues(
formatCurrency,
previousApprovalId,
userProfile,
task,
taskGroup,
taskGroupInstance,
}: ReplaceInjectablesOptions,
): string {
const string = replaceInjectablesWithElementValues(text, {
Expand All @@ -422,6 +443,9 @@ export function replaceInjectablesWithSubmissionValues(
formatNumber,
formatCurrency,
userProfile,
task,
taskGroup,
taskGroupInstance,
})
return CUSTOM_VALUES.reduce((newString, customValue) => {
return newString.replaceAll(
Expand All @@ -439,6 +463,9 @@ export function replaceInjectablesWithSubmissionValues(
previousApprovalId,
submission,
userProfile,
task,
taskGroup,
taskGroupInstance,
}),
)
}, string)
Expand Down