Skip to content
Snippets Groups Projects

Added autofill from database into text boxes, but now data is taken from wrong...

Closed Sahel Cedighi Chafjiri requested to merge dev into FR18
Files
34
+ 153
164
@@ -10,23 +10,32 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
import { initializeApp } from "firebase/app";
import { getFirestore, collection, doc, setDoc, updateDoc, arrayUnion, getDocs, getDoc, where, query, deleteDoc, orderBy, } from "firebase/firestore";
import { getAuth } 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 = {
apiKey: "AIzaSyBIsZq_yOsJljp1woIxndJO1GkW6soh-g4",
authDomain: "company3-456bb.firebaseapp.com",
projectId: "company3-456bb",
storageBucket: "company3-456bb.appspot.com",
messagingSenderId: "310033686522",
appId: "1:310033686522:web:63198bb0db679c98ee4e43",
measurementId: "G-PFLDQY00DB"
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",
storageBucket: "company3-456bb.appspot.com",
messagingSenderId: "310033686522",
appId: "1:310033686522:web:63198bb0db679c98ee4e43",
measurementId: "G-PFLDQY00DB"
};*/
// const firebaseConfig = {
// apiKey: "AIzaSyBIsZq_yOsJljp1woIxndJO1GkW6soh-g4",
// authDomain: "company3-456bb.firebaseapp.com",
// projectId: "company3-456bb",
// storageBucket: "company3-456bb.appspot.com",
// messagingSenderId: "310033686522",
// appId: "1:310033686522:web:63198bb0db679c98ee4e43",
// measurementId: "G-PFLDQY00DB"
// };
const app = initializeApp(firebaseConfig);
export const auth = getAuth();
export const db = getFirestore(app);
@@ -37,7 +46,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");
////////////////////////////////////////Setters///////////////////////////////////////////////////////////////////
// Function to create user a new user in database
@@ -71,13 +80,9 @@ function createUser(hsaId, unitId, firstName, lastName, email, manager, uid) {
}
});
}
//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
function createImprovementWork(id, name, currentStage, content, // Är detta description bör detta förtydligas.
unitId, purpose, //Bör tas bort motsvaras av p2- mål med projektet
patientRiskLevel, area,
// patientRiskLevel: "Green" | "Blue" | "Orange" | "Red"
//Questions in planning stage
p1, p2, p3, p4, p5, p6, p7,
//Questions in Doing (G) stage
@@ -89,11 +94,9 @@ a1, a2, a3, a4) {
return __awaiter(this, void 0, void 0, function* () {
//ID has to be a string
try {
const improvementWorkRef = doc(improvementWorkCollection, id);
yield setDoc(improvementWorkRef, {
id: improvementWorkRef.id,
currentStage: currentStage,
const improvementWorkData = {
name: name,
currentStage: currentStage,
content: content,
purpose: purpose,
dateStarted: new Date(),
@@ -101,7 +104,7 @@ a1, a2, a3, a4) {
completed: false,
ongoing: false,
tasks: [],
// TODO Replace with userImprovementWork collection
// TODO Remove members array
members: [],
patientRiskLevel: patientRiskLevel,
area: area,
@@ -124,7 +127,15 @@ a1, a2, a3, a4) {
a2: a2,
a3: a3,
a4: a4,
});
};
if (id) {
const improvementWorkRef = doc(improvementWorkCollection, id);
yield setDoc(improvementWorkRef, Object.assign({ id: improvementWorkRef.id }, improvementWorkData));
}
else {
const improvementWorkRef = doc(improvementWorkCollection);
yield setDoc(improvementWorkRef, improvementWorkData);
}
}
catch (error) {
console.log("Error creating improvementWork: ", error);
@@ -132,37 +143,6 @@ a1, a2, a3, a4) {
}
});
}
// 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;
// }
// }
function addMemberToTask(userHsaId, taskId, isResponsibleFor) {
return __awaiter(this, void 0, void 0, function* () {
try {
@@ -183,7 +163,7 @@ function addMemberToTask(userHsaId, taskId, isResponsibleFor) {
function addMemberToImprovementWork(userHsaId, improvementWorkId, isOwner, isResponsibleFor) {
return __awaiter(this, void 0, void 0, function* () {
try {
const relationRef = doc(userImprovementWorkCollections);
const relationRef = doc(userImprovementWorkCollection);
yield setDoc(relationRef, {
id: relationRef.id,
userHsaId: userHsaId,
@@ -198,66 +178,26 @@ function addMemberToImprovementWork(userHsaId, improvementWorkId, isOwner, isRes
}
});
}
// 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
function createTaskOnImprovementWork(taskId, improvementWorkId, name, deadlineDate, startDate, stage, content) {
return __awaiter(this, void 0, void 0, function* () {
try {
const ref = doc(taskCollection, taskId);
yield 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.
yield updateDoc(improvementWorkRef, {
tasks: arrayUnion(taskId),
});
const taskData = {
improvementWorkId,
name,
deadlineDate,
startDate,
stage,
content,
members: [], // Initialize with an empty array
};
if (taskId) {
const taskRef = doc(taskCollection, taskId);
yield setDoc(taskRef, Object.assign({ id: taskRef.id }, taskData));
}
else {
const taskRef = doc(taskCollection);
yield setDoc(taskRef, taskData);
}
}
catch (error) {
console.log("Error creating task: ", error);
@@ -305,19 +245,22 @@ function addDocumentToImprovementWork(documentUrl, name, improvementWorkId) {
}
});
}
//create a hospital
// takes in ID, name as string
// throws error if transaction failed
function createHospital(hospitalId, // should be auto?
name) {
return __awaiter(this, void 0, void 0, function* () {
try {
const ref = doc(hospitalCollection, hospitalId);
yield setDoc(ref, {
id: ref.id,
name: name,
units: [],
});
const hospitalData = {
name,
units: []
};
if (hospitalId) {
const hospitalRef = doc(hospitalCollection, hospitalId);
yield setDoc(hospitalRef, Object.assign({ id: hospitalRef.id }, hospitalData));
}
else {
const hospitalRef = doc(hospitalCollection);
yield setDoc(hospitalRef, hospitalData);
}
}
catch (error) {
console.log("Error creating hospital: ", error);
@@ -325,25 +268,23 @@ name) {
}
});
}
//create a unit
// takes in id, hospitals id and name as string
// throws error if transaction failed
function createUnit(unitId, //should be auto?
hospitalId, name) {
return __awaiter(this, void 0, void 0, function* () {
try {
const unitRef = doc(unitCollection, unitId);
yield setDoc(unitRef, {
id: unitRef.id,
hospitalId: hospitalId,
name: name,
members: [],
});
// Atomically add unit to hospital.
const hospitalRef = doc(hospitalCollection, hospitalId);
yield updateDoc(hospitalRef, {
units: arrayUnion(unitRef.id),
});
const unitData = {
hospitalId,
name,
members: []
};
if (unitId) {
const unitRef = doc(unitCollection, unitId);
yield setDoc(unitRef, Object.assign({ id: unitRef.id }, unitData));
}
else {
const unitRef = doc(unitCollection);
yield setDoc(unitRef, unitData);
}
}
catch (error) {
console.log("Error creating unit: ", error);
@@ -351,34 +292,35 @@ hospitalId, name) {
}
});
}
//creates a suggestion
// takes in ID, the creators hsaId, name, content and the responding unitId as string
// throws error if transaction failed
function createSuggestion(id, creatorHsaId, name, area, content, purpose, measure, ideas,
//need: boolean,
unitId, departmentId, isPublic) {
return __awaiter(this, void 0, void 0, function* () {
try {
const suggestionRef = doc(suggestionCollection, id);
yield 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 = {
creatorHsaId,
name,
area,
content,
purpose,
measure,
ideas,
//need,
unitId,
departmentId,
public: isPublic,
upvotes: [],
comments: [],
dateCreated: new Date(),
});
const commentCollection = yield collection(suggestionRef, "comment");
yield doc(commentCollection);
dateCreated: new Date()
};
if (id) {
const suggestionRef = doc(suggestionCollection, id);
yield setDoc(suggestionRef, Object.assign({ id: suggestionRef.id }, suggestionData));
}
else {
const suggestionRef = doc(suggestionCollection);
yield setDoc(suggestionRef, suggestionData);
}
}
catch (error) {
console.log("Error creating suggestion: ", error);
@@ -505,7 +447,7 @@ function getObjectById(collectionPath, documentId) {
const docRef = doc(db, collectionPath, documentId);
const docSnapshot = yield getDoc(docRef);
if (docSnapshot.exists()) {
return docSnapshot.data();
return Object.assign({ id: docSnapshot.id }, docSnapshot.data());
}
else {
console.warn(`No such document! ${collectionPath}/${documentId}`);
@@ -688,6 +630,50 @@ function getImprovementWorks({ id, nolanStage, unitId, completed, ongoing, patie
}
});
}
function getImprovementWorkOwner(improvementWorkId) {
return __awaiter(this, void 0, void 0, function* () {
try {
let constraints = [];
constraints.push(where("improvementWorkId", "==", improvementWorkId));
constraints.push(where("isOwner", "==", true));
const q = query(userImprovementWorkCollection, ...constraints);
const querySnapshot = yield 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);
}
});
}
function getImprovementWorkResponsible(improvementWorkId) {
return __awaiter(this, void 0, void 0, function* () {
try {
let constraints = [];
constraints.push(where("improvementWorkId", "==", improvementWorkId));
constraints.push(where("isResponsibleFor", "==", true));
const q = query(userImprovementWorkCollection, ...constraints);
const querySnapshot = yield 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!
/**
* Asynchronously retrieves user documents from the database based on various filter criteria.
@@ -752,7 +738,7 @@ uid, }) {
function getTasks({
// userHsaId,
// isResponsibleFor,
improvementWorkId, ascendingDates = false, // Descending dates by default
improvementWorkId, stage, ascendingDates = false, // Descending dates by default
}) {
return __awaiter(this, void 0, void 0, function* () {
try {
@@ -767,6 +753,9 @@ improvementWorkId, ascendingDates = false, // Descending dates by default
// })
// );
// }
if (stage) {
constraints.push(where("stage", "==", stage));
}
if (improvementWorkId != undefined) {
constraints.push(where("improvementWorkId", "==", improvementWorkId));
constraints.push(orderBy("startDate", ascendingDates ? "asc" : "desc"));
@@ -834,7 +823,7 @@ function getImprovementWorksForUser(userHsaId) {
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 = yield getDocs(q);
// Fetch each related improvementWork document
const improvementWorks = [];
@@ -856,7 +845,7 @@ function getUsersForImprovementWork(improvementWorkId) {
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 = yield getDocs(q);
// Fetch each related user document
const users = [];
@@ -874,7 +863,7 @@ function getUsersForImprovementWork(improvementWorkId) {
});
}
// export getters to be able to use in other files
export { getObject, getObjectById, getSuggestions, getImprovementWorks, getUsers, getTasks, getUnits, getComments, getHospitals, getImprovementWorksForUser, getUsersForImprovementWork, };
export { getObject, getObjectById, getSuggestions, getImprovementWorks, getUsers, getTasks, getUnits, getComments, getHospitals, getImprovementWorksForUser, getUsersForImprovementWork, getImprovementWorkOwner, getImprovementWorkResponsible, };
////////////////////////////////////DELETING/////////////////////////////////////////////
// delete an instance of a type
function deleteInstance(docType, docId) {
Loading