Skip to content

Commit bdb7be5

Browse files
authored
Merge pull request #14 from DEFRA/fix/DF-648-repeater-columns
Ensures uniqueness of repeater columns, Updated docs
2 parents 5a02639 + a47b23c commit bdb7be5

4 files changed

Lines changed: 39 additions & 4 deletions

File tree

docs/SHAREPOINT_SETUP.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ In order for a submission to get written to a Sharepoint list, the following is
1111
- the values of formId, siteId, listId, and status (live or draft) must exist in the forms-sharepoint-listener config
1212

1313
- the Sharepoint list (denoted by siteId and listId) must contain ALL columns from the source form
14-
- the Sharepoint list column names must match the short description from each question
14+
- the Sharepoint list column names must match the short description from each question **(and the naming is case-sensitive)**
1515

16-
- the short descriptions from the form must all be unique
16+
- the short descriptions from the form must all be unique (within the first 32 characters if spaces are removed)
1717

1818
- the Sharepoint list column datatypes must match according to the ‘Datatype mappings’ table below
1919

@@ -39,6 +39,8 @@ If your form uses repeater pages, you need to setup the appropriate Sharepoint l
3939

4040
- Field area 3
4141

42+
**Note** - there is a limit of 32 characters on the ‘internal’ column name (the name used to lookup the column) so any repeater short descriptions should be no more than 32 characters when the repeat number is added and spaces are removed e.g. ‘My repeater field 25’ (if my max repeaters is 25) which strips to ‘Myrepeaterfield25’ should be no longer than 32 characters.
43+
4244
## Configuration structure
4345

4446
The service configuration (set by the development team or CDP) consists of zero or more rows of the following structure:
@@ -65,6 +67,12 @@ A valid config value which allow two different forms to write to two different S
6567
{"mappings":[{"formId":"695e4ec0e57ae17190adacba","siteId":"071a12a4-5ed0-4a08-bbf6-93a762e89bdb","listId":"0695483b-f344-419f-bc93-ee8aeee1b788","status":"draft"},{"formId":"696104cfab2e01c384cf6382","siteId":"071a12a4-5ed0-4a08-bbf6-93a762e89bdb","listId":"69c339f4-5c06-42ab-89f9-7db121c61fc3","status":"live"}]}
6668
```
6769

70+
## Renaming a Sharepoint column
71+
72+
Although you can rename a Sharepoint column name (for example if you change a question’s short description), the original ‘internal’ Sharepoint column name remains. All you are doing by renaming the Sharepoint column is changing the display name.
73+
74+
So if you need to change a Sharepoint column name to match a question’s short description, you will have to add a new column with the correct name, copy over any data from the original column, then delete the original column.
75+
6876
## Determining the siteId and listId of a Sharepoint list
6977

7078
Normally a Sharepoint list is referred to by a url containing its site name and list name e.g. https://defradev.sharepoint.com/teams/TEAM164UAT/Lists/TestList1/AllItems.aspx

src/service/__stubs__/forms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export const definitionForSharepointTest = buildDefinition({
245245
},
246246
schema: {
247247
min: 1,
248-
max: 5
248+
max: 15
249249
}
250250
}
251251
},

src/service/sharepoint.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,22 @@ export async function saveToSharepointList(message) {
209209

210210
if (hasRepeater(component.page.pageDef)) {
211211
const repeaterName = component.page.pageDef.repeat.options.name
212+
const maxRepeaterItems = /** @type {number} */ (
213+
component.page.pageDef.repeat.schema.max
214+
)
212215
const hasRepeaterData = repeaterName in data.repeaters
213216
const items = hasRepeaterData ? data.repeaters[repeaterName] : []
214217

215218
for (let index = 0; index < items.length; index++) {
216219
const value = getValue(items[index], key, component)
217-
const componentKey = `${getSharepointFieldName(component)}${index + 1}`
220+
const baseComponentKey = getSharepointFieldName(component)
221+
if (baseComponentKey.length + `${maxRepeaterItems}`.length > 32) {
222+
throw new Error(
223+
`Repeater columns plus number index cannot be longer than 32 characters (with spaces stripped) - shortDesc: ${baseComponentKey}${maxRepeaterItems} formId: ${formId}`
224+
)
225+
}
226+
227+
const componentKey = `${baseComponentKey}${index + 1}`
218228

219229
fields.set(componentKey, value)
220230
}

src/service/sharepoint.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ line 3`,
9898
}
9999
})
100100
})
101+
102+
it('should throw if repeater name will blow limits', async () => {
103+
const definition = structuredClone(definitionForSharepointTest)
104+
const page = /** @type {PageQuestion} */ (definition.pages[2])
105+
const component = /** @type {TextFieldComponent} */ (page.components[0])
106+
component.shortDescription = 'Repeater Name That Is Too Long Herexx'
107+
jest.mocked(getFormDefinition).mockResolvedValue(definition)
108+
const message = structuredClone(messageForSharepointTest)
109+
message.meta.formId = 'my-form-id'
110+
await expect(() => saveToSharepointList(message)).rejects.toThrow(
111+
'Repeater columns plus number index cannot be longer than 32 characters (with spaces stripped) - shortDesc: RepeaterNameThatIsTooLongHerexx15 formId: my-form-id'
112+
)
113+
})
101114
})
102115

103116
describe('escapeFieldName', () => {
@@ -143,3 +156,7 @@ line 3`,
143156
})
144157
})
145158
})
159+
160+
/**
161+
* @import { PageQuestion, TextFieldComponent } from '@defra/forms-model'
162+
*/

0 commit comments

Comments
 (0)