-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tech(api): extract Criterion logic into a dedicated model
Co-authored-by: Yvonnick Frin <[email protected]> Co-authored-by: Xavier Carron <[email protected]>
- Loading branch information
1 parent
5a9118d
commit 9ef0271
Showing
5 changed files
with
141 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
export class Criterion { | ||
#data; | ||
|
||
constructor({ data }) { | ||
this.#data = data; | ||
} | ||
|
||
get data() { | ||
return Object.freeze(this.#data); | ||
} | ||
|
||
toDTO() { | ||
return this.#data; | ||
} | ||
|
||
check({ item, comparisonFunction }) { | ||
return Object.keys(this.#data)[comparisonFunction]((key) => { | ||
// TODO: Dés que les quêtes ont été mises à jour il faudra retirer cette ligne | ||
const alterKey = key === 'targetProfileIds' ? 'targetProfileId' : key; | ||
return this.#checkCriterionAttribute({ | ||
criterionAttr: this.#data[key], | ||
dataAttr: item[alterKey], | ||
}); | ||
}); | ||
} | ||
|
||
#checkCriterionAttribute({ criterionAttr, dataAttr }) { | ||
if (Array.isArray(criterionAttr)) { | ||
if (Array.isArray(dataAttr)) { | ||
return criterionAttr.every((valueToTest) => dataAttr.includes(valueToTest)); | ||
} | ||
return criterionAttr.some((valueToTest) => valueToTest === dataAttr); | ||
} | ||
return dataAttr === criterionAttr; | ||
} | ||
} |
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
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,19 @@ | ||
import { Criterion } from '../../../../../src/quest/domain/models/Criterion.js'; | ||
import { expect } from '../../../../test-helper.js'; | ||
|
||
describe('Quest | Unit | Domain | Models | Criterion ', function () { | ||
describe('#toDTO', function () { | ||
it('should return a DTO version of the criterion', function () { | ||
// given | ||
const criterion = new Criterion({ | ||
data: { some: 'awesome', cool: 'stuff' }, | ||
}); | ||
|
||
// when | ||
const DTO = criterion.toDTO(); | ||
|
||
// then | ||
expect(DTO).to.deep.equal({ some: 'awesome', cool: 'stuff' }); | ||
}); | ||
}); | ||
}); |
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