-
Notifications
You must be signed in to change notification settings - Fork 10
Set/Unset a Template as Default #414
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
34b9d2f
Feat: set/unset default template, and its tests
ChengShi-1 61c23f2
chore: remove description in README
ChengShi-1 a25f6ad
chore: rename use cases
ChengShi-1 ab00b04
chore: lint error on renaming
ChengShi-1 d4104c5
chore: remover docker.yml
ChengShi-1 44ac744
fix: depends on reviews
ChengShi-1 8380bf3
Merge branch 'develop' into 408-use-case-to-set-default-template
ChengShi-1 a619488
fix: resolved a flaky test in notification
ChengShi-1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
| import { ROOT_COLLECTION_ID } from '../../../collections/domain/models/Collection' | ||
| import { UseCase } from '../../../core/domain/useCases/UseCase' | ||
| import { ITemplatesRepository } from '../repositories/ITemplatesRepository' | ||
|
|
||
| export class SetTemplateAsDefault implements UseCase<void> { | ||
| private templatesRepository: ITemplatesRepository | ||
|
|
||
| constructor(templatesRepository: ITemplatesRepository) { | ||
| this.templatesRepository = templatesRepository | ||
| } | ||
|
|
||
| /** | ||
| * Sets the default template for the specified collection. | ||
| * | ||
| * @param {number} templateId - Template id to set as default. | ||
| * @param {number | string} [collectionIdOrAlias = ':root'] - A generic collection identifier, which can be either a string (for queries by CollectionAlias), or a number (for queries by CollectionId) | ||
| * If this parameter is not set, the default value is: ':root'. | ||
| */ | ||
| async execute( | ||
| templateId: number, | ||
| collectionIdOrAlias: number | string = ROOT_COLLECTION_ID | ||
| ): Promise<void> { | ||
| return await this.templatesRepository.setTemplateAsDefault(collectionIdOrAlias, templateId) | ||
| } | ||
| } |
This file contains hidden or 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,21 @@ | ||
| import { ROOT_COLLECTION_ID } from '../../../collections/domain/models/Collection' | ||
| import { UseCase } from '../../../core/domain/useCases/UseCase' | ||
| import { ITemplatesRepository } from '../repositories/ITemplatesRepository' | ||
|
|
||
| export class UnsetTemplateAsDefault implements UseCase<void> { | ||
| private templatesRepository: ITemplatesRepository | ||
|
|
||
| constructor(templatesRepository: ITemplatesRepository) { | ||
| this.templatesRepository = templatesRepository | ||
| } | ||
|
|
||
| /** | ||
| * Removes the default template for the specified collection. | ||
| * | ||
| * @param {number | string} [collectionIdOrAlias = ':root'] - A generic collection identifier, which can be either a string (for queries by CollectionAlias), or a number (for queries by CollectionId) | ||
| * If this parameter is not set, the default value is: ':root'. | ||
| */ | ||
| async execute(collectionIdOrAlias: number | string = ROOT_COLLECTION_ID): Promise<void> { | ||
| return await this.templatesRepository.unsetTemplateAsDefault(collectionIdOrAlias) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,86 @@ | ||
| import { ApiConfig } from '../../../src' | ||
| import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' | ||
| import { TestConstants } from '../../testHelpers/TestConstants' | ||
| import { | ||
| createTemplate, | ||
| getTemplatesByCollectionId, | ||
| setTemplateAsDefault, | ||
| unsetTemplateAsDefault | ||
| } from '../../../src/templates' | ||
| import { CreateTemplateDTO } from '../../../src/templates/domain/dtos/CreateTemplateDTO' | ||
| import { MetadataFieldTypeClass } from '../../../src/metadataBlocks/domain/models/MetadataBlock' | ||
| import { deleteDatasetTemplateViaApi } from '../../testHelpers/datasets/datasetTemplatesHelper' | ||
|
|
||
| describe('SetTemplateAsDefault.execute', () => { | ||
| const collectionIdOrAlias = ':root' | ||
|
|
||
| beforeEach(async () => { | ||
| ApiConfig.init( | ||
| TestConstants.TEST_API_URL, | ||
| DataverseApiAuthMechanism.API_KEY, | ||
| process.env.TEST_API_KEY | ||
| ) | ||
| }) | ||
|
|
||
| test('should set the default template for a collection', async () => { | ||
| const templateName = `TestDefaultTemplate-${Date.now()}` | ||
| const templateDto: CreateTemplateDTO = { | ||
| name: templateName, | ||
| isDefault: false, | ||
| fields: [ | ||
| { | ||
| typeName: 'author', | ||
| typeClass: MetadataFieldTypeClass.Compound, | ||
| multiple: true, | ||
| value: [ | ||
| { | ||
| authorName: { | ||
| typeName: 'authorName', | ||
| typeClass: MetadataFieldTypeClass.Primitive, | ||
| value: 'Belicheck, Bill' | ||
| }, | ||
| authorAffiliation: { | ||
| typeName: 'authorIdentifierScheme', | ||
| typeClass: MetadataFieldTypeClass.Primitive, | ||
| value: 'ORCID' | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| instructions: [ | ||
| { | ||
| instructionField: 'author', | ||
| instructionText: 'The author data' | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| const templatesBefore = await getTemplatesByCollectionId.execute(collectionIdOrAlias) | ||
| const originalDefaultTemplateId = | ||
| templatesBefore.find((template) => template.isDefault)?.id ?? null | ||
|
|
||
| await createTemplate.execute(templateDto, collectionIdOrAlias) | ||
| const templatesAfterCreate = await getTemplatesByCollectionId.execute(collectionIdOrAlias) | ||
| const createdTemplate = templatesAfterCreate.find((template) => template.name === templateName) | ||
|
|
||
| if (!createdTemplate) { | ||
| throw new Error('Created template was not found in collection templates.') | ||
| } | ||
|
|
||
| await setTemplateAsDefault.execute(createdTemplate.id, collectionIdOrAlias) | ||
|
|
||
| const templatesAfterSet = await getTemplatesByCollectionId.execute(collectionIdOrAlias) | ||
| const updatedTemplate = templatesAfterSet.find((template) => template.id === createdTemplate.id) | ||
|
|
||
| expect(updatedTemplate?.isDefault).toBe(true) | ||
|
|
||
| if (originalDefaultTemplateId && originalDefaultTemplateId !== createdTemplate.id) { | ||
| await setTemplateAsDefault.execute(originalDefaultTemplateId, collectionIdOrAlias) | ||
| } else if (!originalDefaultTemplateId) { | ||
| await unsetTemplateAsDefault.execute(collectionIdOrAlias) | ||
| } | ||
|
|
||
| await deleteDatasetTemplateViaApi(createdTemplate.id) | ||
| }) | ||
| }) | ||
This file contains hidden or 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,85 @@ | ||
| import { ApiConfig } from '../../../src' | ||
| import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' | ||
| import { TestConstants } from '../../testHelpers/TestConstants' | ||
| import { | ||
| createTemplate, | ||
| getTemplatesByCollectionId, | ||
| setTemplateAsDefault, | ||
| unsetTemplateAsDefault | ||
| } from '../../../src/templates' | ||
| import { CreateTemplateDTO } from '../../../src/templates/domain/dtos/CreateTemplateDTO' | ||
| import { MetadataFieldTypeClass } from '../../../src/metadataBlocks/domain/models/MetadataBlock' | ||
| import { deleteDatasetTemplateViaApi } from '../../testHelpers/datasets/datasetTemplatesHelper' | ||
|
|
||
| describe('UnsetTemplateAsDefault.execute', () => { | ||
| const collectionIdOrAlias = ':root' | ||
|
|
||
| beforeEach(async () => { | ||
| ApiConfig.init( | ||
| TestConstants.TEST_API_URL, | ||
| DataverseApiAuthMechanism.API_KEY, | ||
| process.env.TEST_API_KEY | ||
| ) | ||
| }) | ||
|
|
||
| test('should remove the default template from a collection', async () => { | ||
| const templateName = `TestUnsetTemplateAsDefault-${Date.now()}` | ||
| const templateDto: CreateTemplateDTO = { | ||
| name: templateName, | ||
| isDefault: false, | ||
| fields: [ | ||
| { | ||
| typeName: 'author', | ||
| typeClass: MetadataFieldTypeClass.Compound, | ||
| multiple: true, | ||
| value: [ | ||
| { | ||
| authorName: { | ||
| typeName: 'authorName', | ||
| typeClass: MetadataFieldTypeClass.Primitive, | ||
| value: 'Belicheck, Bill' | ||
| }, | ||
| authorAffiliation: { | ||
| typeName: 'authorIdentifierScheme', | ||
| typeClass: MetadataFieldTypeClass.Primitive, | ||
| value: 'ORCID' | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| instructions: [ | ||
| { | ||
| instructionField: 'author', | ||
| instructionText: 'The author data' | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| const templatesBefore = await getTemplatesByCollectionId.execute(collectionIdOrAlias) | ||
| const originalDefaultTemplateId = | ||
| templatesBefore.find((template) => template.isDefault)?.id ?? null | ||
|
|
||
| await createTemplate.execute(templateDto, collectionIdOrAlias) | ||
| const templatesAfterCreate = await getTemplatesByCollectionId.execute(collectionIdOrAlias) | ||
| const createdTemplate = templatesAfterCreate.find((template) => template.name === templateName) | ||
|
|
||
| if (!createdTemplate) { | ||
| throw new Error('Created template was not found in collection templates.') | ||
| } | ||
|
|
||
| await setTemplateAsDefault.execute(createdTemplate.id, collectionIdOrAlias) | ||
| await unsetTemplateAsDefault.execute(collectionIdOrAlias) | ||
|
|
||
| const templatesAfterRemove = await getTemplatesByCollectionId.execute(collectionIdOrAlias) | ||
| const hasDefaultTemplate = templatesAfterRemove.some((template) => template.isDefault) | ||
|
|
||
| expect(hasDefaultTemplate).toBe(false) | ||
|
|
||
| if (originalDefaultTemplateId && originalDefaultTemplateId !== createdTemplate.id) { | ||
| await setTemplateAsDefault.execute(originalDefaultTemplateId, collectionIdOrAlias) | ||
| } | ||
|
|
||
| await deleteDatasetTemplateViaApi(createdTemplate.id) | ||
|
ChengShi-1 marked this conversation as resolved.
Outdated
|
||
| }) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.