From d03b738337a3f830f2b5a3c082f82c062350e139 Mon Sep 17 00:00:00 2001
From: 9sneha-n <9sneha.n@gmail.com>
Date: Wed, 8 May 2024 10:33:45 +0530
Subject: [PATCH] feat: do not allow creation of multiple facility forms in
 same OU

---
 src/domain/usecases/SaveFormDataUseCase.ts | 34 +++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/src/domain/usecases/SaveFormDataUseCase.ts b/src/domain/usecases/SaveFormDataUseCase.ts
index f87b8853..f01a93b4 100644
--- a/src/domain/usecases/SaveFormDataUseCase.ts
+++ b/src/domain/usecases/SaveFormDataUseCase.ts
@@ -33,6 +33,38 @@ export class SaveFormDataUseCase {
         const ouId =
             surveyFormType === "PPSSurveyForm" && orgUnitId === "" ? GLOBAL_OU_ID : orgUnitId;
 
+        //Do not allow creation of multiple Prevalence Facility Level Forms for the same facility.
+        if (surveyFormType === "PrevalenceFacilityLevelForm") {
+            return this.surveyReporsitory
+                .getSurveys(surveyFormType, programId, ouId, false)
+                .flatMap(surveys => {
+                    if (surveys.length > 0) {
+                        return Future.error(
+                            new Error(
+                                "Prevalence Facility Level Form already exists for this facility."
+                            )
+                        );
+                    } else
+                        return this.saveFormData(
+                            surveyFormType,
+                            questionnaire,
+                            ouId,
+                            programId,
+                            eventId
+                        );
+                });
+        }
+
+        return this.saveFormData(surveyFormType, questionnaire, ouId, programId, eventId);
+    }
+
+    saveFormData = (
+        surveyFormType: SURVEY_FORM_TYPES,
+        questionnaire: Questionnaire,
+        ouId: string,
+        programId: string,
+        eventId: string | undefined = undefined
+    ): FutureData<void> => {
         return this.surveyReporsitory
             .saveFormData(questionnaire, "CREATE_AND_UPDATE", ouId, eventId, programId)
             .flatMap((surveyId: Id | undefined) => {
@@ -46,7 +78,7 @@ export class SaveFormDataUseCase {
                     return Future.success(undefined);
                 }
             });
-    }
+    };
 
     saveCustomASTGuidelineToDatastore = (
         surveyId: Id,