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-35848 # Fixed task replaceables #65

Merged
merged 1 commit into from
Oct 27, 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
85 changes: 22 additions & 63 deletions src/replaceCustomValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type ReplaceInjectablesOptions = ReplaceInjectablesBaseOptions & {
previousApprovalId: string | undefined
}

const CUSTOM_VALUES = [
const SUBMISSION_VALUES = [
{
string: '{INFO_PAGE_ID}',
value: ({ form }: ReplaceInjectablesOptions) => form.id.toString(),
Expand Down Expand Up @@ -85,18 +85,27 @@ const CUSTOM_VALUES = [
value: ({ previousApprovalId }: ReplaceInjectablesOptions) =>
previousApprovalId || '',
},
]

const ELEMENT_VALUES: Array<{
string: string
value: (options: ReplaceInjectablesBaseOptions) => string
}> = [
{
string: '{USER:email}',
value: ({ userProfile }) => userProfile?.email || '',
},
{
string: '{TASK_NAME}',
value: ({ task }: ReplaceInjectablesOptions) => task?.name || '',
value: ({ task }) => task?.name || '',
},
{
string: '{TASK_GROUP_NAME}',
value: ({ taskGroup }: ReplaceInjectablesOptions) => taskGroup?.name || '',
value: ({ taskGroup }) => taskGroup?.name || '',
},
{
string: '{TASK_GROUP_INSTANCE_LABEL}',
value: ({ taskGroupInstance }: ReplaceInjectablesOptions) =>
taskGroupInstance?.label || '',
value: ({ taskGroupInstance }) => taskGroupInstance?.label || '',
},
]

Expand Down Expand Up @@ -328,15 +337,9 @@ export function replaceInjectablesWithElementValues(
formElements: FormTypes.FormElement[]
} & ReplaceInjectablesBaseOptions,
): string {
const keys: Array<keyof MiscTypes.UserProfile> = ['email']
// User based values should be replaced with an empty string if
// there is no user profile or if the property does not have a value
keys.forEach((key) => {
text = text.replaceAll(
`{USER:${key}}`,
options.userProfile?.[key]?.toString() || '',
)
})
text = ELEMENT_VALUES.reduce((newString, customValue) => {
return newString.replaceAll(customValue.string, customValue.value(options))
}, text)

const matchesElement = text.match(ElementWYSIWYGRegex)
if (!matchesElement) {
Expand Down Expand Up @@ -416,57 +419,13 @@ export function replaceInjectablesWithElementValues(
*/
export function replaceInjectablesWithSubmissionValues(
text: string,
{
form,
submission,
externalId,
submissionId,
submissionTimestamp,
formatDate,
formatDateTime,
formatTime,
formatNumber,
formatCurrency,
previousApprovalId,
userProfile,
task,
taskGroup,
taskGroupInstance,
}: ReplaceInjectablesOptions,
options: ReplaceInjectablesOptions,
): string {
const string = replaceInjectablesWithElementValues(text, {
formElements: form.elements,
submission,
formatDate,
formatDateTime,
formatTime,
formatNumber,
formatCurrency,
userProfile,
task,
taskGroup,
taskGroupInstance,
formElements: options.form.elements,
...options,
})
return CUSTOM_VALUES.reduce((newString, customValue) => {
return newString.replaceAll(
customValue.string,
customValue.value({
form,
submissionTimestamp,
externalId,
submissionId,
formatDate,
formatDateTime,
formatTime,
formatNumber,
formatCurrency,
previousApprovalId,
submission,
userProfile,
task,
taskGroup,
taskGroupInstance,
}),
)
return SUBMISSION_VALUES.reduce((newString, customValue) => {
return newString.replaceAll(customValue.string, customValue.value(options))
}, string)
}
67 changes: 65 additions & 2 deletions tests/submissionService.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
import { FormTypes } from '@oneblink/types'
import { replaceInjectablesWithSubmissionValues } from '../src/submissionService'
import { FormTypes, ScheduledTasksTypes } from '@oneblink/types'
import {
replaceInjectablesWithElementValues,
replaceInjectablesWithSubmissionValues,
} from '../src/submissionService'

describe('replaceInjectablesWithSubmissionValues()', () => {
const task: ScheduledTasksTypes.Task = {
id: 1,
name: 'Replace the food',
formsAppEnvironmentId: 1,
organisationId: 'string',
schedule: {
startDate: new Date().toISOString(),
endDate: new Date().toISOString(),
recurrence: {
interval: 'DAY',
},
},
actionIds: [],
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
}
const taskGroup: ScheduledTasksTypes.TaskGroup = {
id: 1,
name: 'Cage',
taskIds: [task.id],
formsAppEnvironmentId: task.formsAppEnvironmentId,
organisationId: task.organisationId,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
}
const taskGroupInstance: ScheduledTasksTypes.TaskGroupInstance = {
taskGroupId: taskGroup.id,
taskGroupInstanceId: 'taskGroupInstanceId',
label: 'Snake Cage',
createdAt: new Date().toISOString(),
}
const form: FormTypes.Form = {
id: 1,
name: 'string',
Expand Down Expand Up @@ -84,6 +118,9 @@ describe('replaceInjectablesWithSubmissionValues()', () => {
username: 'person1',
email: 'person@email.com',
},
task,
taskGroup,
taskGroupInstance,
}
describe('Form redirect URL', () => {
test('should replace all instances of {ELEMENT} with correct property value', () => {
Expand Down Expand Up @@ -205,4 +242,30 @@ describe('replaceInjectablesWithSubmissionValues()', () => {
'person@email.com;autre_person@email.com;person@email.com',
)
})

describe('task based props', () => {
const text =
'Task: {TASK_NAME} | Task Group: {TASK_GROUP_NAME} | Task Group Instance: {TASK_GROUP_INSTANCE_LABEL}'
const expected =
'Task: Replace the food | Task Group: Cage | Task Group Instance: Snake Cage'

test('should be replaced for element values', () => {
const result = replaceInjectablesWithElementValues(text, {
...baseOptions,
formElements: baseOptions.form.elements,
submission: {},
})

expect(result).toEqual(expected)
})

test('should replace task based props', () => {
const result = replaceInjectablesWithSubmissionValues(text, {
...baseOptions,
submission: {},
})

expect(result).toEqual(expected)
})
})
})