Skip to content

Commit

Permalink
fix post locations/:org #67
Browse files Browse the repository at this point in the history
  • Loading branch information
Sigura committed Mar 12, 2020
1 parent 84b9609 commit e56abf2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/server/models/Location.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,14 @@ export async function createLocations(

export async function create(
params,
org,
) {
if (Array.isArray(params)) {
return Promise.reduce(
params,
async (p, pp) => {
try {
await create(pp);
await create(pp, org);
} catch (e) {
console.error('v1:create', e);
throw e;
Expand All @@ -151,7 +152,7 @@ export async function create(
}

const {
company_token: token = 'UNKNOWN',
company_token: token = org || 'UNKNOWN',
location: list = [],
device = { model: 'UNKNOWN', uuid: 'UNKNOWN' },
} = params;
Expand Down
6 changes: 3 additions & 3 deletions src/server/routes/api-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ router.post('/locations', checkAuth(verify), async (req, res) => {
router.post('/locations/:company_token', checkAuth(verify), async (req, res) => {
const { deviceId, org } = req.jwt;
let { companyId } = req.jwt;
!companyId && ({ company_id: companyId } = await getDevice({ id: deviceId, org }) || {});
const { company_token: orgId } = req.params;
!companyId && ({ company_id: companyId } = await getDevice({ id: deviceId, org: org || orgId }) || {});

// eslint-disable-next-line no-console
console.info(
Expand All @@ -359,10 +360,9 @@ router.post('/locations/:company_token', checkAuth(verify), async (req, res) =>
const data = isEncryptedRequest(req)
? decrypt(req.body.toString())
: req.body;
data.company_token = org;

try {
await create(data);
await create(data, org || orgId);
return res.send({ success: true });
} catch (err) {
if (err instanceof AccessDeniedError) {
Expand Down
8 changes: 4 additions & 4 deletions src/server/routes/firebase-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,18 @@ router.post('/locations', checkAuth(verify), async (req, res) => {
*/
router.post('/locations/:company_token', checkAuth(verify), async (req, res) => {
const { org } = req.jwt;
const { company_id: orgId } = req.params;
const { company_token: orgId } = req.params;

// eslint-disable-next-line no-console
console.info(
'v3',
'locations:post'.green,
'org:name'.green,
org,
org || orgId,
'device:id'.green,
);

if (isDDosCompany(org) || isDDosCompany(orgId)) {
if ((org && isDDosCompany(org)) || (orgId && isDDosCompany(orgId))) {
return return1Gbfile(res);
}

Expand All @@ -355,7 +355,7 @@ router.post('/locations/:company_token', checkAuth(verify), async (req, res) =>
: req.body;

try {
await create(data);
await create(data, orgId || org);
return res.send({ success: true });
} catch (err) {
if (err instanceof AccessDeniedError) {
Expand Down
3 changes: 1 addition & 2 deletions src/server/routes/site-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,9 @@ router.post('/locations/:company_token', getAuth(verify), async (req, res) => {
const data = isEncryptedRequest(req)
? decrypt(req.body.toString())
: req.body;
data.company_token = org;

try {
await create(data);
await create(data, org);

return res.send({ success: true });
} catch (err) {
Expand Down
24 changes: 15 additions & 9 deletions tests/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,30 @@ export const regData = {

export const location = {
is_moving: false,
uuid: '8a21f59c-c7d8-43ed-ac6d-8b23cea7c7d7',
timestamp: '2019-11-17T19:14:25.776Z',
odometer: 4616.5,
uuid: '03f4aa4c-ed00-4390-9e82-49f0c5799940',
timestamp: '2020-03-12T19:26:12.020Z',
timestampMeta: {
time: 1584041172020,
systemTime: 1584041176933,
systemClockElaspsedRealtime: 584834172,
elapsedRealtime: 584829260,
},
odometer: 6454829,
coords: {
latitude: 45.519264,
longitude: -73.616931,
accuracy: 15.2,
latitude: 45.5192402,
longitude: -73.6169874,
accuracy: 15.9,
speed: -1,
heading: -1,
altitude: 41.8,
altitude: 43.8,
},
activity: {
type: 'still',
confidence: 100,
},
battery: {
is_charging: true,
level: 0.92,
level: 0.98,
},
extras: { setCurrentPosition: true },
extras: { getCurrentPosition: true },
};

0 comments on commit e56abf2

Please sign in to comment.