-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert questionnaire answers to legacy format and add tests for conv…
…ersion utility
- Loading branch information
1 parent
d4981bd
commit 9147398
Showing
4 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
apps/app/src/features/questionnaire/server/util/convert-to-legacy-format.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { GrowiDeploymentType, GrowiServiceType } from '@growi/core/dist/consts'; | ||
import type { IGrowiInfo } from '@growi/core/dist/interfaces'; | ||
import { GrowiWikiType } from '@growi/core/dist/interfaces'; | ||
import { describe, test, expect } from 'vitest'; | ||
|
||
import { AttachmentMethodType } from '../../../../interfaces/attachment'; | ||
import type { IGrowiAppAdditionalInfo, IGrowiAppInfoLegacy } from '../../interfaces/growi-app-info'; | ||
|
||
import { convertToLegacyFormat } from './convert-to-legacy-format'; | ||
|
||
describe('convertToLegacyFormat', () => { | ||
test('should return same object when input is already in legacy format', () => { | ||
const growiInfoLegacy: IGrowiAppInfoLegacy = { | ||
version: '1.0.0', | ||
appSiteUrl: 'https://example.com', | ||
appSiteUrlHashed: 'hashedUrl', | ||
type: GrowiServiceType.cloud, | ||
wikiType: GrowiWikiType.open, | ||
deploymentType: GrowiDeploymentType.others, | ||
|
||
// legacy properties | ||
installedAt: new Date(), | ||
installedAtByOldestUser: new Date(), | ||
currentUsersCount: 1, | ||
currentActiveUsersCount: 1, | ||
attachmentType: AttachmentMethodType.local, | ||
}; | ||
|
||
const legacyData = { | ||
someData: 'test', | ||
growiInfo: growiInfoLegacy, | ||
}; | ||
|
||
const result = convertToLegacyFormat(legacyData); | ||
expect(result).toStrictEqual(legacyData); | ||
}); | ||
|
||
test('should convert new format to legacy format', () => { | ||
const growiInfo: IGrowiInfo<IGrowiAppAdditionalInfo> = { | ||
version: '1.0.0', | ||
appSiteUrl: 'https://example.com', | ||
appSiteUrlHashed: 'hashedUrl', | ||
type: GrowiServiceType.cloud, | ||
wikiType: GrowiWikiType.open, | ||
deploymentType: GrowiDeploymentType.others, | ||
|
||
additionalInfo: { | ||
installedAt: new Date(), | ||
installedAtByOldestUser: new Date(), | ||
currentUsersCount: 1, | ||
currentActiveUsersCount: 1, | ||
attachmentType: AttachmentMethodType.local, | ||
}, | ||
}; | ||
const newFormatData = { | ||
someData: 'test', | ||
growiInfo, | ||
}; | ||
|
||
const growiInfoLegacy: IGrowiAppInfoLegacy = { | ||
version: '1.0.0', | ||
appSiteUrl: 'https://example.com', | ||
appSiteUrlHashed: 'hashedUrl', | ||
type: GrowiServiceType.cloud, | ||
wikiType: GrowiWikiType.open, | ||
deploymentType: GrowiDeploymentType.others, | ||
|
||
// legacy properties | ||
installedAt: new Date(), | ||
installedAtByOldestUser: new Date(), | ||
currentUsersCount: 1, | ||
currentActiveUsersCount: 1, | ||
attachmentType: AttachmentMethodType.local, | ||
}; | ||
const expected = { | ||
someData: 'test', | ||
growiInfo: growiInfoLegacy, | ||
}; | ||
|
||
const result = convertToLegacyFormat(newFormatData); | ||
expect(result).toStrictEqual(expected); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
apps/app/src/features/questionnaire/server/util/convert-to-legacy-format.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { IGrowiAppInfoLegacy } from '../../interfaces/growi-app-info'; | ||
|
||
|
||
type IHasGrowiAppInfoLegacy<T> = T & { | ||
growiInfo: IGrowiAppInfoLegacy; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function hasGrowiAppInfoLegacy<T extends { growiInfo: any }>(data: T): data is IHasGrowiAppInfoLegacy<T> { | ||
return !('additionalInfo' in data.growiInfo); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function convertToLegacyFormat<T extends { growiInfo: any }>(questionnaireAnswer: T): IHasGrowiAppInfoLegacy<T> { | ||
if (!hasGrowiAppInfoLegacy(questionnaireAnswer)) { | ||
const { additionalInfo, ...rest } = questionnaireAnswer.growiInfo; | ||
assert(additionalInfo != null); | ||
|
||
return { | ||
...questionnaireAnswer, | ||
growiInfo: { | ||
...rest, | ||
...additionalInfo, | ||
}, | ||
}; | ||
} | ||
|
||
return questionnaireAnswer; | ||
} |