diff --git a/client/Database/database.ts b/client/Database/database.ts index e1b62be8d58a51f5d196d0bdc40e3337986297ab..aab221c29ef268826b7d21a37d8f737d44c08ecd 100644 --- a/client/Database/database.ts +++ b/client/Database/database.ts @@ -20,16 +20,16 @@ import { } from "firebase/firestore"; import { getAuth, createUserWithEmailAndPassword } from "firebase/auth"; -// const firebaseConfig = { -// apiKey: "AIzaSyDqkaVZMr11J4mfnR0auqoQilZyPfyD-x4", -// authDomain: "c3solutions-dc109.firebaseapp.com", -// projectId: "c3solutions-dc109", -// storageBucket: "c3solutions-dc109.appspot.com", -// messagingSenderId: "902143402555", -// appId: "1:902143402555:web:29213e81de0a0cba38b21e", -// }; - -const firebaseConfig = { + const firebaseConfig = { + apiKey: "AIzaSyDqkaVZMr11J4mfnR0auqoQilZyPfyD-x4", + authDomain: "c3solutions-dc109.firebaseapp.com", + projectId: "c3solutions-dc109", + storageBucket: "c3solutions-dc109.appspot.com", + messagingSenderId: "902143402555", + appId: "1:902143402555:web:29213e81de0a0cba38b21e", + }; + +/*const firebaseConfig = { apiKey: "AIzaSyBIsZq_yOsJljp1woIxndJO1GkW6soh-g4", authDomain: "company3-456bb.firebaseapp.com", projectId: "company3-456bb", @@ -37,7 +37,7 @@ const firebaseConfig = { messagingSenderId: "310033686522", appId: "1:310033686522:web:63198bb0db679c98ee4e43", measurementId: "G-PFLDQY00DB" -}; +};*/ const app = initializeApp(firebaseConfig); export const auth = getAuth(); @@ -49,7 +49,7 @@ const taskCollection = collection(db, "task"); const hospitalCollection = collection(db, "hospital"); const suggestionCollection = collection(db, "suggestion"); const knowledgeDocumentCollection = collection(db, "knowledgeDocument"); -const userImprovementWorkCollections = collection(db, "userImprovementWork"); +const userImprovementWorkCollection = collection(db, "userImprovementWork"); const userTaskCollection = collection(db, "userTask"); interface improvementWorkType { @@ -123,8 +123,44 @@ async function createUser( //Adds a new improvementWork to the database // takes in Id, name, currentStage in PDSA, content, unitId refers to as string and if completed as boolean // throws error if transaction failed +interface improvementWork { + id?: string, + name: string, + currentStage: string, + content: string, + purpose: string, + dateStarted: Date, + unitId: string, + completed: boolean, + ongoing: boolean, + tasks: [], + // TODO Replace with userImprovementWork collection + members: [], + patientRiskLevel: string, + area: string, + p1: string, + p2: string, + p3: string, + p4: string, + p5: string, + p6: string, + p7: string, + g1: string, + g2: string, + g3: string, + g4: string, + g5: string, + s1: string, + s2: string, + s3: string, + a1: string, + a2: string, + a3: string, + a4: string, +} + async function createImprovementWork( - id: string, + id: string | null, name: string, currentStage: string, content: string, // Är detta description bör detta förtydligas. @@ -132,7 +168,6 @@ async function createImprovementWork( purpose: string, //Bör tas bort motsvaras av p2- mål med projektet patientRiskLevel: string, area: string, - // patientRiskLevel: "Green" | "Blue" | "Orange" | "Red" //Questions in planning stage p1: string, p2: string, @@ -159,12 +194,9 @@ async function createImprovementWork( ): Promise<void> { //ID has to be a string try { - const improvementWorkRef = doc(improvementWorkCollection, id); - - await setDoc(improvementWorkRef, { - id: improvementWorkRef.id, - currentStage: currentStage, + const improvementWorkData : improvementWork = { name: name, + currentStage: currentStage, content: content, purpose: purpose, dateStarted: new Date(), @@ -172,7 +204,7 @@ async function createImprovementWork( completed: false, ongoing: false, tasks: [], - // TODO Replace with userImprovementWork collection + // TODO Remove members array members: [], patientRiskLevel: patientRiskLevel, area: area, @@ -195,45 +227,21 @@ async function createImprovementWork( a2: a2, a3: a3, a4: a4, - }); + } + + if (id) { + const improvementWorkRef = doc(improvementWorkCollection, id); + await setDoc(improvementWorkRef, {id: improvementWorkRef.id, ...improvementWorkData}); + } else { + const improvementWorkRef = doc(improvementWorkCollection); + await setDoc(improvementWorkRef, improvementWorkData); + } } catch (error) { console.log("Error creating improvementWork: ", error); throw error; } } -// Replace with userTask collection (OLD) -//adds a member to task -// takes in ID, the added members hsaId, task id as string and if person is responsible as boolean -// throws error if transaction failed -// async function addMemberToTask( -// userHsaId: string, -// taskId: string, -// isResponsibleFor: boolean -// ): Promise<void> { -// try { -// // Atomically add a member to task. -// const taskRef = doc(taskCollection, taskId); -// await updateDoc(taskRef, { -// members: arrayUnion({ -// userHsaId: userHsaId, -// isResponsibleFor: isResponsibleFor, -// }), -// }); -// // Atomically add a task to user. -// const userRef = doc(userCollection, userHsaId); -// await updateDoc(userRef, { -// tasks: arrayUnion({ -// taskId: taskId, -// isResponsibleFor: isResponsibleFor, -// }), -// }); -// } catch (error) { -// console.log("Error adding member: ", error); -// throw error; -// } -// } - async function addMemberToTask( userHsaId: string, taskId: string, @@ -262,7 +270,7 @@ async function addMemberToImprovementWork( isResponsibleFor: boolean ): Promise<void> { try { - const relationRef = doc(userImprovementWorkCollections); + const relationRef = doc(userImprovementWorkCollection); await setDoc(relationRef, { id: relationRef.id, @@ -277,51 +285,22 @@ async function addMemberToImprovementWork( throw error; } } -// TODO Replace with userImprovementWork collection (OLD) -// add a member to a improvementWork -// takes in id, users HSAid, improvementWorks id as string and if is owner, is responsible as boolean -// throws error if transaction failed -// async function addMemberToImprovementWork( -// userHsaId: string, -// improvementWorkId: string, -// isOwner: boolean, -// isResponsibleFor: boolean -// ): Promise<void> { -// try { -// const improvementWorkRef = doc( -// improvementWorkCollection, -// improvementWorkId -// ); -// // return; -// // Atomically update the members array in the improvementWork document -// await updateDoc(improvementWorkRef, { -// members: arrayUnion({ -// userHsaId: userHsaId, -// isOwner: isOwner, -// isResponsibleFor: isResponsibleFor, -// }), -// }); -// const userRef = doc(userCollection, userHsaId); -// // Atomically add improvementWork to user -// await updateDoc(userRef, { -// improvementWorks: arrayUnion({ -// improvementWorkId: improvementWorkId, -// isOwner: isOwner, -// isResponsibleFor: isResponsibleFor, -// }), -// }); -// } catch (error) { -// console.log("Error adding member: ", error); -// throw error; -// } -// } - //create task to improvementWork // takes in improvementWorkId, name, deadline date, start date, stage in PDSA and content as string // throws error if transaction failed +interface Task { + id?: string; // Optional because it can be auto-generated + improvementWorkId: string; + name: string; + deadlineDate: Date; + startDate: Date; + stage: string; + content: string; + members: string[]; // Assuming this is an array of user IDs +} async function createTaskOnImprovementWork( - taskId: string, + taskId: string | null, improvementWorkId: string, name: string, deadlineDate: Date, @@ -330,25 +309,24 @@ async function createTaskOnImprovementWork( content: string ): Promise<void> { try { - const ref = doc(taskCollection, taskId); - await setDoc(ref, { - id: ref.id, - improvementWorkId: improvementWorkId, - name: name, - deadlineDate: deadlineDate, - startDate: startDate, - stage: stage, - content: content, - members: [], - }); - const improvementWorkRef = doc( - improvementWorkCollection, - improvementWorkId - ); - // Atomically add task to the tasks array field. - await updateDoc(improvementWorkRef, { - tasks: arrayUnion(taskId), - }); + const taskData: Task = { + improvementWorkId, + name, + deadlineDate, + startDate, + stage, + content, + members: [], // Initialize with an empty array + }; + + if (taskId) { + const taskRef = doc(taskCollection, taskId); + await setDoc(taskRef, {id: taskRef.id, ...taskData}); + } else { + const taskRef = doc(taskCollection); + await setDoc(taskRef, taskData); + } + } catch (error) { console.log("Error creating task: ", error); throw error; @@ -405,17 +383,28 @@ async function addDocumentToImprovementWork( //create a hospital // takes in ID, name as string // throws error if transaction failed +interface Hospital { + id?: string; // Optional because it can be auto-generated + name: string; + units: string[]; // Assuming this is an array of unit IDs +} async function createHospital( - hospitalId: string, // should be auto? + hospitalId: string | null, // should be auto? name: string ): Promise<void> { try { - const ref = doc(hospitalCollection, hospitalId); - await setDoc(ref, { - id: ref.id, - name: name, - units: [], - }); + const hospitalData : Hospital = { + name, + units: [] + } + + if (hospitalId) { + const hospitalRef = doc(hospitalCollection, hospitalId); + await setDoc(hospitalRef, {id: hospitalRef.id, ...hospitalData}); + } else { + const hospitalRef = doc(hospitalCollection); + await setDoc(hospitalRef, hospitalData); + } } catch (error) { console.log("Error creating hospital: ", error); throw error; @@ -425,24 +414,31 @@ async function createHospital( //create a unit // takes in id, hospitals id and name as string // throws error if transaction failed +interface Unit { + id?: string; // Optional because it can be auto-generated + hospitalId: string; + name: string; + members: string[]; // Assuming this is an array of user IDs +} async function createUnit( - unitId: string, //should be auto? + unitId: string | null, //should be auto? hospitalId: string, name: string ): Promise<void> { try { - const unitRef = doc(unitCollection, unitId); - await setDoc(unitRef, { - id: unitRef.id, - hospitalId: hospitalId, - name: name, - members: [], - }); - // Atomically add unit to hospital. - const hospitalRef = doc(hospitalCollection, hospitalId); - await updateDoc(hospitalRef, { - units: arrayUnion(unitRef.id), - }); + const unitData : Unit = { + hospitalId, + name, + members: [] + } + + if (unitId) { + const unitRef = doc(unitCollection, unitId); + await setDoc(unitRef, {id: unitRef.id, ...unitData}); + } else { + const unitRef = doc(unitCollection); + await setDoc(unitRef, unitData); + } } catch (error) { console.log("Error creating unit: ", error); throw error; @@ -452,8 +448,25 @@ async function createUnit( //creates a suggestion // takes in ID, the creators hsaId, name, content and the responding unitId as string // throws error if transaction failed +interface Suggestion { + id?: string; // Optional because it can be auto-generated + creatorHsaId: string; + name: string; + area: string; + content: string; + purpose: string; + measure: string; + ideas: string; + //need: boolean + unitId: string; + departmentId: string; + public: boolean; + upvotes: string[]; + comments: Comment[]; + dateCreated: Date; +} async function createSuggestion( - id: string, + id: string | null, creatorHsaId: string, name: string, area: string, @@ -467,27 +480,30 @@ async function createSuggestion( isPublic: boolean ): Promise<void> { try { - const suggestionRef = doc(suggestionCollection, id); - await setDoc(suggestionRef, { - id: suggestionRef.id, - creatorHsaId: creatorHsaId, - name: name, - area: area, - content: content, - purpose: purpose, - measure: measure, - ideas: ideas, - //need: need, - unitId: unitId, - departmentId: departmentId, + const suggestionData : Suggestion = { + creatorHsaId, + name, + area, + content, + purpose, + measure, + ideas, + //need, + unitId, + departmentId, public: isPublic, upvotes: [], comments: [], - dateCreated: new Date(), - }); + dateCreated: new Date() + } - const commentCollection = await collection(suggestionRef, "comment"); - await doc(commentCollection); + if (id) { + const suggestionRef = doc(suggestionCollection, id); + await setDoc(suggestionRef, {id: suggestionRef.id, ...suggestionData}); + } else { + const suggestionRef = doc(suggestionCollection); + await setDoc(suggestionRef, suggestionData); + } } catch (error) { console.log("Error creating suggestion: ", error); throw error; @@ -640,7 +656,7 @@ async function getObjectById( const docSnapshot = await getDoc(docRef); if (docSnapshot.exists()) { - return docSnapshot.data(); + return {id: docSnapshot.id, ...docSnapshot.data()}; } else { console.warn(`No such document! ${collectionPath}/${documentId}`); return null; @@ -893,6 +909,48 @@ async function getImprovementWorks({ } } +async function getImprovementWorkOwner(improvementWorkId: string) { + try { + let constraints : any[] = []; + constraints.push(where("improvementWorkId", "==", improvementWorkId)); + constraints.push(where("isOwner", "==", true)); + + const q = query(userImprovementWorkCollection, ...constraints); + const querySnapshot = await getDocs(q); + + if (querySnapshot.empty) { + console.log("Improvement work ", improvementWorkId, " has no owner"); + return null; + } else { + const owner = querySnapshot.docs[0].data().userHsaId; + return owner; + } + } catch (error) { + console.log(error); + } +} + +async function getImprovementWorkResponsible(improvementWorkId: string) { + try { + let constraints : any[] = []; + constraints.push(where("improvementWorkId", "==", improvementWorkId)); + constraints.push(where("isResponsibleFor", "==", true)); + + const q = query(userImprovementWorkCollection, ...constraints); + const querySnapshot = await getDocs(q); + + if (querySnapshot.empty) { + console.log("Improvement work ", improvementWorkId, " has no responsible"); + return null; + } else { + const responsible = querySnapshot.docs[0].data().userHsaId; + return responsible; + } + } catch (error) { + console.log(error); + } +} + // TODO getUsersForTask! /** @@ -1084,7 +1142,7 @@ async function getImprovementWorksForUser(userHsaId: string): Promise<DocumentDa try { console.log("Getting improvementworks for user: ", userHsaId); // Query the userImprovementWork collection for relations involving this user - const q = query(userImprovementWorkCollections, where('userHsaId', '==', userHsaId)); + const q = query(userImprovementWorkCollection, where('userHsaId', '==', userHsaId)); const querySnapshot = await getDocs(q); // Fetch each related improvementWork document @@ -1105,7 +1163,7 @@ async function getUsersForImprovementWork(improvementWorkId: string): Promise<Do try { console.log("Getting users for improvementwork: ", improvementWorkId); // Query the userImprovementWork collection for relations involving this improvementWork - const q = query(userImprovementWorkCollections, where('improvementWorkId', '==', improvementWorkId)); + const q = query(userImprovementWorkCollection, where('improvementWorkId', '==', improvementWorkId)); const querySnapshot = await getDocs(q); // Fetch each related user document @@ -1137,6 +1195,8 @@ export { getHospitals, getImprovementWorksForUser, getUsersForImprovementWork, + getImprovementWorkOwner, + getImprovementWorkResponsible, }; ////////////////////////////////////DELETING///////////////////////////////////////////// diff --git a/client/Make_Suggestion_Code/makeSuggestion.ts b/client/Make_Suggestion_Code/makeSuggestion.ts index cee04f7ea30773b5d2eb7eeeac375e1529e21b81..aa2a54d986819ee30475efa3e63bfd74b06e676e 100644 --- a/client/Make_Suggestion_Code/makeSuggestion.ts +++ b/client/Make_Suggestion_Code/makeSuggestion.ts @@ -135,12 +135,14 @@ export async function submitHandler() { var user = await getUsers({uid : uid}); console.log(user); console.log(user[0].uid); + console.log(user[0].id); + console.log(user[0].creatorHsaId); try { - if (user[0].uid) { + if (user[0].id) { createSuggestion( - user[0].uid, - "<Department>", + null, + user[0].id, //The unique identifyer for the user, not the idea nameInput, areaInput, contentInput,