Implement function findUseCaseByNeed
@@ -453,40 +453,66 @@ export function getAllDetails() {
* @param needImm
*/
export function findUseCaseByNeed(needImm) {
- //FIXME: IMPLEMENT CORRECTLY
console.debug("Trying to find a useCase that corresponds with:", needImm);
const seeksTypes =
getIn(needImm, ["seeks", "type"]) &&
getIn(needImm, ["seeks", "type"]).remove("won:Need");
+
const contentTypes =
getIn(needImm, ["content", "type"]) &&
getIn(needImm, ["content", "type"]).remove("won:Need");
- if (
- ((seeksTypes && seeksTypes.size > 0) ||
- (contentTypes && contentTypes.size > 0)) &&
- hasSubElements(useCases)
- ) {
+ if (hasSubElements(useCases)) {
const useCasesImm = Immutable.fromJS(useCases);
if (useCasesImm && useCasesImm.size > 0) {
- useCasesImm.map(useCase => {
- const useCaseContentTypes = getIn(useCase, [
- "draft",
- "content",
- "type",
- ]);
- const useCaseSeeksTypes = getIn(useCase, ["draft", "seeks", "type"]);
- if (
- (!useCaseContentTypes ||
- useCaseContentTypes.contains(contentTypes)) &&
- (!useCaseSeeksTypes || useCaseSeeksTypes.contains(seeksTypes))
- ) {
- console.debug("FOUND Matching UseCase!!!", useCase.toJS());
- return true;
+ const hasExactMatchingTypes = (useCase, types, branch) => {
+ const typesSize = types ? types.size : 0;
+ const ucTypes = useCase.getIn(["draft", branch, "type"]);
+ const ucTypesSize = ucTypes ? ucTypes.size : 0;
+
+ if (typesSize != ucTypesSize) {
+ return false;
+ }
+
+ const hasTypes = typesSize > 0;
+
+ return !hasTypes || (ucTypes && ucTypes.isSubset(types));
+ };
+
+ let matchingUseCases = useCasesImm.filter(useCase =>
+ hasExactMatchingTypes(useCase, contentTypes, "content")
+ );
+
+ if (matchingUseCases.size > 1) {
+ //If there are multiple matched found based on the content type(s) alone we refine based on the seeks type as well
+ matchingUseCases = matchingUseCases.filter(useCase =>
+ hasExactMatchingTypes(useCase, seeksTypes, "seeks")
+ );
+ }
+
+ if (matchingUseCases && matchingUseCases.size > 1) {
+ console.warn(
+ "Found multiple matching UseCases for: ",
+ needImm,
+ " matching UseCases: ",
+ matchingUseCases,
+ " -> returning undefined"
+ );
+ return undefined;
+ } else if (matchingUseCases && matchingUseCases.size == 1) {
+ return matchingUseCases.first().toJS();
+ } else {
+ console.warn(
+ "Found no matching UseCase for:",
+ needImm,
+ " within, ",
+ useCases,
+ " -> returning undefined"
+ );
+ return undefined;
}
- });
}
}
