diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a4542e9..89ebecf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + +- `goodToGoService` + ## [8.5.1] - 2025-07-10 ### Added diff --git a/package-lock.json b/package-lock.json index e4553efc..ddd1ca1c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "@microsoft/microsoft-graph-types": "^2.40.0", "@oneblink/release-cli": "^3.3.1-beta.4", "@oneblink/types": "github:oneblink/types", + "@spotto/contract": "^1.0.53", "@types/jest": "^29.5.14", "@types/lodash": "^4.17.16", "@typescript-eslint/eslint-plugin": "^8.28.0", @@ -1773,7 +1774,7 @@ }, "node_modules/@oneblink/types": { "version": "1.0.0", - "resolved": "git+ssh://git@github.com/oneblink/types.git#af5b55d5274278db584b0dfbbe473ea18bc8679c", + "resolved": "git+ssh://git@github.com/oneblink/types.git#42c24e1bb1b7a14bf30b01222df04e5483d97868", "dev": true, "license": "GPL-3.0-only", "dependencies": { @@ -1898,6 +1899,13 @@ "@sinonjs/commons": "^3.0.0" } }, + "node_modules/@spotto/contract": { + "version": "1.0.53", + "resolved": "https://registry.npmjs.org/@spotto/contract/-/contract-1.0.53.tgz", + "integrity": "sha512-NobHK5JFJJwt6Q2q11264PwfbPAqxSi0MamLu2K6wmR9X7AyoimDscC/QbHEoVSkk2+tNi4LfSCKPWa6kH1hNA==", + "dev": true, + "license": "ISC" + }, "node_modules/@types/babel__core": { "version": "7.20.5", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.5.tgz", diff --git a/package.json b/package.json index 913a36ed..b21b64f9 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@microsoft/microsoft-graph-types": "^2.40.0", "@oneblink/release-cli": "^3.3.1-beta.4", "@oneblink/types": "github:oneblink/types", + "@spotto/contract": "^1.0.53", "@types/jest": "^29.5.14", "@types/lodash": "^4.17.16", "@typescript-eslint/eslint-plugin": "^8.28.0", diff --git a/src/goodToGoService.ts b/src/goodToGoService.ts new file mode 100644 index 00000000..258ee1f2 --- /dev/null +++ b/src/goodToGoService.ts @@ -0,0 +1,36 @@ +import { GetFieldResponse } from '@spotto/contract' +import { SubmissionEventTypes } from '@oneblink/types' + +import { ResourceDefinition } from './types/resource-definitions' + +export type GoodToGoFieldResourceDefinition = + ResourceDefinition + +function generateCommonConfig(fieldDefinition: GetFieldResponse) { + return { + id: fieldDefinition.id, + goodToGoCustomFieldName: fieldDefinition.name, + isRequired: !!fieldDefinition.required, + displayName: fieldDefinition.label ?? fieldDefinition.name, + } +} + +export function generateGoodToGoFieldResourceDefinitions( + fieldDefinitions: GetFieldResponse[], +) { + return fieldDefinitions.reduce( + (memo, fieldDefinition) => { + switch (fieldDefinition.inputType) { + case 'SINGLELINE': + memo.push({ + type: 'TEXT_SINGLE_LINE', + ...generateCommonConfig(fieldDefinition), + }) + break + } + + return memo + }, + [], + ) +} diff --git a/src/index.ts b/src/index.ts index be491383..a6880289 100644 --- a/src/index.ts +++ b/src/index.ts @@ -120,3 +120,14 @@ export * as sharepointService from './sharepointService' * ``` */ export * as freshdeskService from './freshdeskService' + +/** + * ## GoodToGo Service + * + * Helper functions for handling GoodToGo + * + * ```js + * import { goodToGoService } from '@oneblink/sdk-core' + * ``` + */ +export * as goodToGoService from './goodToGoService' diff --git a/tests/goodToGoService.test.ts b/tests/goodToGoService.test.ts new file mode 100644 index 00000000..c0d94811 --- /dev/null +++ b/tests/goodToGoService.test.ts @@ -0,0 +1,63 @@ +import { GetFieldResponse } from '@spotto/contract' +import { generateGoodToGoFieldResourceDefinitions } from '../src/goodToGoService' + +describe('generateGoodToGoFieldResourceDefinitions', () => { + it('should map TEXT_SINGLE_LINE fields correctly', () => { + const fields: GetFieldResponse[] = [ + { + id: 'a', + name: 'text', + label: 'Text', + required: true, + dataType: 'STRING', + inputType: 'SINGLELINE', + }, + ] + + expect(generateGoodToGoFieldResourceDefinitions(fields)).toEqual([ + { + type: 'TEXT_SINGLE_LINE', + id: 'a', + goodToGoCustomFieldName: 'text', + isRequired: true, + displayName: 'Text', + }, + ]) + }) + + it('should skip fields that are not yet supported', () => { + const fields: GetFieldResponse[] = [ + { + id: 'd', + name: 'checkbox', + label: 'Checkbox', + dataType: 'STRING', + inputType: 'CHECKBOXES', + }, + ] + + expect(generateGoodToGoFieldResourceDefinitions(fields)).toEqual([]) + }) + + it('should use field name if label is not provided', () => { + const fields: GetFieldResponse[] = [ + { + id: 'a', + name: 'text', + required: true, + dataType: 'STRING', + inputType: 'SINGLELINE', + }, + ] + + expect(generateGoodToGoFieldResourceDefinitions(fields)).toEqual([ + { + type: 'TEXT_SINGLE_LINE', + id: 'a', + goodToGoCustomFieldName: 'text', + isRequired: true, + displayName: 'text', + }, + ]) + }) +})