diff --git a/frontend/src/store/GhgModule.ts b/frontend/src/store/GhgModule.ts index 9a31f25f..95ff8274 100644 --- a/frontend/src/store/GhgModule.ts +++ b/frontend/src/store/GhgModule.ts @@ -15,7 +15,7 @@ import { RootState } from "."; import { updateMetaFieldsForUpdate } from "./documentUtils"; import { GridItem } from "./GhgReferenceGridModule"; import { ReferenceItemInterface } from "./GhgReferenceModule"; -import { CouchUser } from "./UserModule"; +import { CouchUser, Roles } from "./UserModule"; const MSG_DB_DOES_NOT_EXIST = "Please, init your database"; const MSG_USER_NOT_PRESENT = "Could not find user information"; @@ -311,27 +311,38 @@ const actions: ActionTree = { const user = context.rootGetters["UserModule/user"] as CouchUser; // context.commit("ADD_DOC", generateNewProject(newGhg, user)); const value = generateNewProject(newGhg, user) as any; - const remoteDB = context.state.localCouch?.remoteDB; - if (remoteDB) { + let db = context.state.localCouch?.remoteDB; + if (user.roles?.includes(Roles.guest)) { + db = context.state.localCouch?.localDB; + } + if (db) { delete value.isUNHCR; value._id = value?._id ?? value?.id ?? uuidv4(); delete value.id; // just in case we forgot to remove it delete value._rev; // just in case we forgot to remove it - return remoteDB.post(value).then((response) => { + return db.post(value).then((response) => { context.commit("NEW_ASSESSEMENT", true); return context.dispatch("getDoc", response.id); }); } }, removeDoc: (context: ActionContext, id) => { - const remoteDB = context.state.localCouch?.remoteDB; - if (!remoteDB) { + const user = context.rootGetters["UserModule/user"] as CouchUser; + + let db = context.state.localCouch?.remoteDB; + if (user.roles?.includes(Roles.guest)) { + db = context.state.localCouch?.localDB; + } + if (!db) { throw new Error(MSG_DB_DOES_NOT_EXIST); } - return remoteDB + return db .get(id) .then(function (doc: PouchDB.Core.ExistingDocument) { - return remoteDB.put({ ...doc, _deleted: true }); + if (!db) { + throw new Error(MSG_DB_DOES_NOT_EXIST); + } + return db.put({ ...doc, _deleted: true }); }) .finally(() => { context.commit("REMOVE_ASSESSEMENT", true); @@ -348,24 +359,32 @@ const actions: ActionTree = { const newValue = updateMetaFieldsForUpdate(value, user); context.commit("SET_PROJECT", newValue); }, - getDoc: (context: ActionContext, id) => { + getDoc: async (context: ActionContext, id) => { const db = context.state.localCouch?.remoteDB; - if (db) { + const localDB = context.state.localCouch?.localDB; // for guest user only + if (localDB) { context.commit("SET_PROJECT_LOADING", true); - return db - .get(id) - .then(function (result) { - context.commit("SET_PROJECT", result); - return result; - }) - .catch(function (err: Error) { - context.commit("SET_PROJECT_LOADING", false); - err.message = `${err?.message} ${id}`; - throw err; - }) - .finally(() => { - context.commit("SET_PROJECT_LOADING", false); - }); + try { + const result = await localDB.get(id); + context.commit("SET_PROJECT", result); + return result; + } catch (errL: unknown) { + try { + if (db) { + const result = await db.get(id); + context.commit("SET_PROJECT", result); + return result; + } + } catch (err: unknown) { + if (err instanceof Error) { + context.commit("SET_PROJECT_LOADING", false); + err.message = `${err?.message} ${id}`; + } + } + throw errL; + } finally { + context.commit("SET_PROJECT_LOADING", false); + } } else { context.commit("SET_PROJECT_LOADING", false); throw new Error(MSG_DB_DOES_NOT_EXIST); @@ -388,7 +407,11 @@ const actions: ActionTree = { // no need for "SET_PROJECT" it should only be done by getDoc context.commit("SET_PROJECT_LOADING", true); - const db = context.state.localCouch?.remoteDB; + + let db = context.state.localCouch?.remoteDB; + if (user.roles?.includes(Roles.guest)) { + db = context.state.localCouch?.localDB; + } if (db) { // first retrieve latest rev // const doc = await db.get(newValue.id); diff --git a/frontend/src/store/ShelterModule.ts b/frontend/src/store/ShelterModule.ts index f5243091..fffaaf64 100644 --- a/frontend/src/store/ShelterModule.ts +++ b/frontend/src/store/ShelterModule.ts @@ -16,7 +16,7 @@ import { Module, MutationTree, } from "vuex"; -import { CouchUser } from "./UserModule"; +import { CouchUser, Roles } from "./UserModule"; const DB_NAME = "shelter_projects_1698666594213623"; const MSG_DB_DOES_NOT_EXIST = "Please, init your database"; @@ -206,11 +206,15 @@ const actions: ActionTree = { const user = context.rootGetters["UserModule/user"] as CouchUser; if (user.name) { const value = generateNewShelter(name, user); - const remoteDB = context.state.localCouch?.remoteDB; - if (!remoteDB) { + let db = context.state.localCouch?.remoteDB; + + if (user.roles?.includes(Roles.guest)) { + db = context.state.localCouch?.localDB; + } + if (!db) { throw new Error(MSG_DB_DOES_NOT_EXIST); } - return remoteDB.post(value).then(() => { + return db.post(value).then(() => { context.commit("ADD_DOC", value); }); } @@ -220,6 +224,9 @@ const actions: ActionTree = { value: Shelter ) => { const user = context.rootGetters["UserModule/user"] as CouchUser; + if (user.roles?.includes(Roles.guest)) { + return; + } if (user.name) { // retrieve real document first let newValue = await context.dispatch("getDoc", value._id); @@ -238,6 +245,10 @@ const actions: ActionTree = { } }, removeDoc: (context: ActionContext, id) => { + const user = context.rootGetters["UserModule/user"] as CouchUser; + if (user.roles?.includes(Roles.guest)) { + return; + } context.commit("REMOVE_DOC", id); }, updateDoc: async (context: ActionContext, value) => { @@ -246,8 +257,13 @@ const actions: ActionTree = { value.updated_at = new Date().toISOString(); value.updated_by = user.name; const computedShelter = computeShelter(value); - const remoteDB = context.state.localCouch?.remoteDB; - if (!remoteDB) { + // const remoteDB = context.state.localCouch?.remoteDB; + let db = context.state.localCouch?.remoteDB; + + if (user.roles?.includes(Roles.guest)) { + db = context.state.localCouch?.localDB; + } + if (!db) { throw new Error(MSG_DB_DOES_NOT_EXIST); } if (!context.getters["shelterLoading"]) { @@ -257,7 +273,7 @@ const actions: ActionTree = { }, 300); timeoutIds.push(timeId); try { - const response = await remoteDB.put(computedShelter); + const response = await db.put(computedShelter); computedShelter._rev = response.rev; // we were calling the get Doc too many times // const newValue = await context.dispatch("getDoc", response.id); @@ -321,8 +337,25 @@ const actions: ActionTree = { true, { root: true } ); - let result: Shelter | undefined = - await context.state.localCouch?.remoteDB.get(id); + const db = context.state.localCouch?.remoteDB; + const localDB = context.state.localCouch?.localDB; // for guest user only + let result: Shelter | undefined; + if (localDB) { + try { + result = await localDB.get(id); + } catch (errL: unknown) { + try { + if (db) { + result = await db.get(id); + } + } catch (err: unknown) { + if (err instanceof Error) { + err.message = `${err?.message} ${id}`; + } + throw err; + } + } + } if (result) { result = computeShelter(result); context.commit("SET_SHELTER", result); diff --git a/frontend/src/utils/couchdb.ts b/frontend/src/utils/couchdb.ts index 01f7ae1d..a1741e86 100644 --- a/frontend/src/utils/couchdb.ts +++ b/frontend/src/utils/couchdb.ts @@ -183,6 +183,7 @@ export function createSyncDatabase(name: string): SyncDatabase { export class SyncDatabase { public remoteDB: PouchDB.Database; + public localDB: PouchDB.Database; private onChangeListener: PouchDB.Core.Changes | undefined; constructor(name: string) { @@ -198,6 +199,7 @@ export class SyncDatabase { } : undefined, }); + this.localDB = new PouchDB(name); this.remoteDB = remoteDB; } diff --git a/frontend/src/views/green_house_gaz/GreenHouseGazDashboard.vue b/frontend/src/views/green_house_gaz/GreenHouseGazDashboard.vue index 8d0cbb47..104371ed 100644 --- a/frontend/src/views/green_house_gaz/GreenHouseGazDashboard.vue +++ b/frontend/src/views/green_house_gaz/GreenHouseGazDashboard.vue @@ -20,13 +20,7 @@ - + $mdiPlusBox New assessment diff --git a/frontend/src/views/green_house_gaz/GreenHouseGazItem/SurveysItem.vue b/frontend/src/views/green_house_gaz/GreenHouseGazItem/SurveysItem.vue index 89422c3c..8358383c 100644 --- a/frontend/src/views/green_house_gaz/GreenHouseGazItem/SurveysItem.vue +++ b/frontend/src/views/green_house_gaz/GreenHouseGazItem/SurveysItem.vue @@ -45,6 +45,7 @@ ? "" : "(This assessment is the Reference for this site so it cannot be modified.)" }} + (Guest mode: read only) diff --git a/frontend/src/views/shelter_sustainability/ShelterSustainabilityList.vue b/frontend/src/views/shelter_sustainability/ShelterSustainabilityList.vue index 500b1e0a..6c0355f1 100644 --- a/frontend/src/views/shelter_sustainability/ShelterSustainabilityList.vue +++ b/frontend/src/views/shelter_sustainability/ShelterSustainabilityList.vue @@ -120,7 +120,6 @@