From 21df92a7d7ed81f50fa0d67bb143ec21dc0e4da3 Mon Sep 17 00:00:00 2001 From: samisa-abeysinghe Date: Sat, 22 Oct 2022 05:39:20 +0530 Subject: [PATCH] Allow prospect/consent duplicates if not active --- api/applicant_consent_data.bal | 6 ++++++ api/main.bal | 17 +++++++++++++++-- api/prospect_data.bal | 6 ++++++ api/types.bal | 2 ++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/api/applicant_consent_data.bal b/api/applicant_consent_data.bal index 6a06f27..035479b 100644 --- a/api/applicant_consent_data.bal +++ b/api/applicant_consent_data.bal @@ -42,6 +42,12 @@ public isolated service class ApplicantConsentData { return new AvinyaTypeData(id); } + isolated resource function get active() returns boolean? { + lock { + return self.applicant_consent.active; + } + } + isolated resource function get phone() returns int? { lock { return self.applicant_consent.phone; diff --git a/api/main.bal b/api/main.bal index 1b5cad2..a5b6f6c 100644 --- a/api/main.bal +++ b/api/main.bal @@ -64,8 +64,9 @@ service graphql:Service /graphql on new graphql:Listener(4000) { ApplicantConsent|error? applicantConsentRaw = db_client -> queryRow( `SELECT * FROM avinya_db.applicant_consent - WHERE email = ${applicantConsent.email} OR - phone = ${applicantConsent.phone};` + WHERE (email = ${applicantConsent.email} OR + phone = ${applicantConsent.phone}) AND + active = TRUE;` ); if(applicantConsentRaw is ApplicantConsent) { @@ -128,6 +129,18 @@ service graphql:Service /graphql on new graphql:Listener(4000) { } remote function add_prospect(Prospect prospect) returns ProspectData|error? { + Prospect|error? prospectRaw = db_client -> queryRow( + `SELECT * + FROM avinya_db.applicant_consent + WHERE (email = ${prospect.email} OR + phone = ${prospect.phone}) AND + active = TRUE;` + ); + + if(prospectRaw is Prospect) { + return error("Prospect already exists. The phone or the email you provided is already used by another prospect"); + } + sql:ExecutionResult res = check db_client->execute( `INSERT INTO avinya_db.prospect ( name, diff --git a/api/prospect_data.bal b/api/prospect_data.bal index cb6304d..33e5321 100644 --- a/api/prospect_data.bal +++ b/api/prospect_data.bal @@ -30,6 +30,12 @@ public isolated service class ProspectData { self.prospect = consent_raw.cloneReadOnly(); } + isolated resource function get active() returns boolean? { + lock { + return self.prospect.active; + } + } + isolated resource function get phone() returns int? { lock { return self.prospect.phone; diff --git a/api/types.bal b/api/types.bal index b671f0a..499419d 100644 --- a/api/types.bal +++ b/api/types.bal @@ -228,6 +228,7 @@ public type WorkExperienceEvaluation record {| public type ApplicantConsent record {| readonly string? record_type = "applicant_consent"; int id?; + boolean? active; int? organization_id; int? avinya_type_id; int? person_id; @@ -247,6 +248,7 @@ public type ApplicantConsent record {| public type Prospect record {| readonly string? record_type = "prospect"; int id?; + boolean? active; string? name; int? phone; string? email;