Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 61 additions & 13 deletions src/DeAIManifesto_backend/main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ actor {
return result;
};

stable var signeesToConfirmStorageStable : [(Text, ManifestoSignee)] = [];
var signeesToConfirmStorage : HashMap.HashMap<Text, ManifestoSignee> = HashMap.HashMap(0, Text.equal, Text.hash);

private func putTemporaryManifestoSignee(signee : ManifestoSignee) : Text {
signeesToConfirmStorage.put(signee.emailAddress, signee);
return signee.emailAddress;
};

private func getTemporaryManifestoSignee(emailAddress : Text) : ?ManifestoSignee {
let result = signeesToConfirmStorage.get(emailAddress);
return result;
};

public func submit_signup_form(submittedSignUpForm : SignUpFormInput) : async Text {
if (submittedSignUpForm.name.size() > 40 or submittedSignUpForm.emailAddress.size() > 40) {
return "Wrong input. Please correct it.";
Expand All @@ -58,22 +71,27 @@ actor {
};
switch(getManifestoSignee(submittedSignUpForm.emailAddress)) {
case null {
// New signee
let newSignee : ManifestoSignee = {
emailAddress: Text = submittedSignUpForm.emailAddress;
name: Text = submittedSignUpForm.name;
signedAt: Nat64 = Nat64.fromNat(Int.abs(Time.now()));
title: ?Text = submittedSignUpForm.title;
organization: ?Text = submittedSignUpForm.organization;
};
let result = putManifestoSignee(newSignee);
if (result != newSignee.emailAddress) {
return "There was an error. Please try again.";
switch(getTemporaryManifestoSignee(submittedSignUpForm.emailAddress)) {
case null {
// New signee
let newSignee : ManifestoSignee = {
emailAddress: Text = submittedSignUpForm.emailAddress;
name: Text = submittedSignUpForm.name;
signedAt: Nat64 = Nat64.fromNat(Int.abs(Time.now()));
title: ?Text = submittedSignUpForm.title;
organization: ?Text = submittedSignUpForm.organization;
};
let result = putTemporaryManifestoSignee(newSignee); // Signee needs to be confirmed before showing up on live page
if (result != newSignee.emailAddress) {
return "There was an error. Please try again.";
};
return "Successfully signed!";
};
case _ { return "Already signed! Your name will show up as a signee soon."; };
};
return "Successfully signed!";
};
case _ { return "Already signed!"; };
};
};
};

// Function to get all individual signee names
Expand Down Expand Up @@ -107,6 +125,19 @@ actor {
return [];
};

// Function for custodian to get individual signees to be confirmed
public shared query ({ caller }) func get_temporary_manifesto_signees() : async [(Text, ManifestoSignee)] {
// don't allow anonymous Principal
if (Principal.isAnonymous(caller)) {
return [];
};
// Only Principals registered as custodians can access this function
if (Principal.isController(caller)) {
return Iter.toArray(signeesToConfirmStorage.entries());
};
return [];
};

// Function for custodian to get the number of individual signees
public shared query ({ caller }) func get_number_of_manifesto_signees() : async Int {
// don't allow anonymous Principal
Expand Down Expand Up @@ -134,13 +165,30 @@ actor {
return false;
};

// Function for custodian to delete an individual signee from the temporary signees
public shared({ caller }) func delete_temporary_manifesto_signee(emailAddress : Text) : async Bool {
// don't allow anonymous Principal
if (Principal.isAnonymous(caller)) {
return false;
};
// Only Principals registered as custodians can access this function
if (Principal.isController(caller)) {
signeesToConfirmStorage.delete(emailAddress);
return true;
};
return false;
};

// Upgrade Hooks
system func preupgrade() {
signeesStorageStable := Iter.toArray(signeesStorage.entries());
signeesToConfirmStorageStable := Iter.toArray(signeesToConfirmStorage.entries());
};

system func postupgrade() {
signeesStorage := HashMap.fromIter(Iter.fromArray(signeesStorageStable), signeesStorageStable.size(), Text.equal, Text.hash);
signeesStorageStable := [];
signeesToConfirmStorage := HashMap.fromIter(Iter.fromArray(signeesToConfirmStorageStable), signeesToConfirmStorageStable.size(), Text.equal, Text.hash);
signeesToConfirmStorageStable := [];
};
};