From a88f374264a5ab29134274fb27a3b536a8e7323f Mon Sep 17 00:00:00 2001 From: Artur Dudnik Date: Fri, 6 Mar 2020 03:25:52 +0300 Subject: [PATCH] fix select issue #67 --- src/client/components/WrappedViewport.js | 6 +++--- src/server/models/Device.js | 9 ++++++--- src/server/models/Org.js | 4 +--- src/server/routes/site-api.js | 7 ++++--- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/client/components/WrappedViewport.js b/src/client/components/WrappedViewport.js index 6a0a0f5..9c2de3f 100644 --- a/src/client/components/WrappedViewport.js +++ b/src/client/components/WrappedViewport.js @@ -28,10 +28,10 @@ const WrappedViewport = ({ useEffect(() => { const action = !hasToken && !!shared ? showAuthDialog() - : getDefaultJwt(token); + : (!!token && getDefaultJwt(token)); - !hasData && store.dispatch(action); - }); + !hasData && action && store.dispatch(action); + }, [hasToken]); return hasToken || !shared ? (!loading ? : ) : ; diff --git a/src/server/models/Device.js b/src/server/models/Device.js index 26073e0..cc8cc15 100644 --- a/src/server/models/Device.js +++ b/src/server/models/Device.js @@ -29,12 +29,15 @@ export async function getDevice({ } export async function getDevices(params, isAdmin) { - const { org } = params || {}; - if (!isAdmin && !org) { + const { org, companyId } = params || {}; + + if (!isAdmin && !(org || companyId)) { return []; } - const whereConditions = { company_token: org }; + const whereConditions = isAdmin + ? { company_id: companyId } + : { company_token: org }; const result = await DeviceModel.findAll({ where: whereConditions, diff --git a/src/server/models/Org.js b/src/server/models/Org.js index a4ef0cc..0159977 100644 --- a/src/server/models/Org.js +++ b/src/server/models/Org.js @@ -37,11 +37,9 @@ export async function findOrCreate({ org }) { } export const findOne = async ({ org }) => { - const co = await CompanyModel.findOne({ + const company = await CompanyModel.findOne({ where: { company_token: org }, - order: [['recorded_at', desc]], raw: true, }); - const company = co ? hydrate(co) : null; return company; }; diff --git a/src/server/routes/site-api.js b/src/server/routes/site-api.js index 5c25734..65127d8 100644 --- a/src/server/routes/site-api.js +++ b/src/server/routes/site-api.js @@ -45,8 +45,9 @@ router.get('/company_tokens', checkAuth(verify), async (req, res) => { */ router.get('/devices', checkAuth(verify), async (req, res) => { const { org } = req.jwt; + const { company_id: companyId } = req.query; try { - const devices = await getDevices({ ...req.query, org }, isAdmin(req.jwt)); + const devices = await getDevices({ companyId, org }, isAdmin(req.jwt)); res.send(devices); } catch (err) { console.error('v1', '/devices', err); @@ -250,10 +251,10 @@ router.post('/jwt', async (req, res) => { const { org } = req.body || {}; try { - const { id } = findOne(org) || {}; + const { id } = await findOne({ org }) || {}; if (!id) { - res.status(401).send({ org, error: 'Org not found' }); + return res.status(401).send({ org, error: 'Org not found' }); } const jwtInfo = {