Skip to content

Commit

Permalink
Fix tenant setting and blank screen (#1920)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspergrom authored Dec 1, 2023
1 parent d354fc8 commit 42d756c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
34 changes: 33 additions & 1 deletion backend/src/api/auth/authMe.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
import { Error403 } from '@crowd/common'
import { RedisCache } from '@crowd/redis'
import { FeatureFlagRedisKey } from '@crowd/types'
import AutomationRepository from '@/database/repositories/automationRepository'

export default async (req, res) => {
if (!req.currentUser || !req.currentUser.id) {
await req.responseHandler.error(req, res, new Error403(req.language))
return
}

await req.responseHandler.success(req, res, req.currentUser)
const payload = req.currentUser

const csvExportCountCache = new RedisCache(
FeatureFlagRedisKey.CSV_EXPORT_COUNT,
req.redis,
req.log,
)

const memberEnrichmentCountCache = new RedisCache(
FeatureFlagRedisKey.MEMBER_ENRICHMENT_COUNT,
req.redis,
req.log,
)

payload.tenants = await Promise.all(
payload.tenants.map(async (tenantUser) => {
tenantUser.tenant.dataValues = {
...tenantUser.tenant.dataValues,
csvExportCount: Number(await csvExportCountCache.get(tenantUser.tenant.id)) || 0,
automationCount:
Number(await AutomationRepository.countAllActive(req.database, tenantUser.tenant.id)) ||
0,
memberEnrichmentCount:
Number(await memberEnrichmentCountCache.get(tenantUser.tenant.id)) || 0,
}
return tenantUser
}),
)

await req.responseHandler.success(req, res, payload)
}
16 changes: 4 additions & 12 deletions frontend/src/modules/auth/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import {
} from '@/modules/auth/auth-socket';

export default {
async doInit({ commit, dispatch }) {
async doInit({ commit, dispatch, state }) {
try {
const token = AuthToken.get();
if (token) {
connectSocket(token);
const currentUser = await AuthService.fetchMe();
commit('AUTH_INIT_SUCCESS', { currentUser });
dispatch('doRefreshTenant');

state.loadingInit = false;
return currentUser;
}

Expand All @@ -40,12 +41,6 @@ export default {
}
},

async doRefreshTenant({ state }) {
if (state.currentTenant.id !== AuthCurrentTenant.get()) {
state.currentTenant = await TenantService.fetchAndApply();
}
},

doWaitUntilInit({ getters }) {
if (!getters.loadingInit) {
return Promise.resolve();
Expand Down Expand Up @@ -114,7 +109,6 @@ export default {
commit('AUTH_SUCCESS', {
currentUser,
});
dispatch('doRefreshTenant');

router.push('/');
})
Expand Down Expand Up @@ -142,7 +136,6 @@ export default {
commit('AUTH_SUCCESS', {
currentUser: currentUser || null,
});
dispatch('doRefreshTenant');
router.push('/');
})
.catch((error) => {
Expand All @@ -162,7 +155,7 @@ export default {
router.push('/auth/signin');
},

doRefreshCurrentUser({ commit, dispatch }) {
doRefreshCurrentUser({ commit }) {
const token = AuthToken.get();
if (token) {
return AuthService.fetchMe()
Expand All @@ -171,7 +164,6 @@ export default {
currentUser,
});

dispatch('doRefreshTenant');
return currentUser;
})
.catch((error) => {
Expand Down
13 changes: 10 additions & 3 deletions frontend/src/modules/auth/store/mutations.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import formbricks, { setupFormbricks } from '@/plugins/formbricks';
import AuthCurrentTenant from '@/modules/auth/auth-current-tenant';

export default {
CURRENT_USER_REFRESH_SUCCESS(state, payload) {
state.currentUser = payload.currentUser || null;
state.currentTenant = AuthCurrentTenant.selectAndSaveOnStorageFor(
payload.currentUser,
);
},

AUTH_START(state) {
Expand All @@ -11,7 +15,9 @@ export default {

AUTH_SUCCESS(state, payload) {
state.currentUser = payload.currentUser || null;

state.currentTenant = AuthCurrentTenant.selectAndSaveOnStorageFor(
payload.currentUser,
);
state.loading = false;

if (state.currentUser) {
Expand Down Expand Up @@ -102,8 +108,9 @@ export default {
AUTH_INIT_SUCCESS(state, payload) {
state.currentUser = payload.currentUser || null;

state.loadingInit = false;

state.currentTenant = AuthCurrentTenant.selectAndSaveOnStorageFor(
payload.currentUser,
);
if (state.currentUser) {
// initialize Formbricks
setupFormbricks(state.currentUser);
Expand Down

0 comments on commit 42d756c

Please sign in to comment.