Skip to content

Commit

Permalink
review uuid usage #67
Browse files Browse the repository at this point in the history
  • Loading branch information
Sigura committed Mar 12, 2020
1 parent e56abf2 commit 2a450cc
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 20 deletions.
17 changes: 8 additions & 9 deletions src/server/firebase/Location.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,17 @@ export async function getLatestLocation(params, isAdmin) {
}


export async function createLocation(location, deviceInfo, org, batch) {
export async function createLocation(location, device, org, batch) {
const now = new Date();
const { uuid: deviceId } = deviceInfo;

const currentDevice = await findOrCreate(org, { ...deviceInfo });
const { uuid: deviceId } = device;

console.info(
'v3:location:create'.green,
'org:name'.green,
org,
'org'.green,
'device:id'.green,
currentDevice.device_id,
device.device_id,
);

const orgRef = firestore
Expand Down Expand Up @@ -156,13 +154,13 @@ export async function createLocations(
await batch.commit();
}

export async function create(params) {
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('v3:create', e);
throw e;
Expand All @@ -173,10 +171,11 @@ export async function create(params) {
}

const {
company_token: token = 'UNKNOWN',
company_token: token = org || 'UNKNOWN',
location: list = [],
device = { model: 'UNKNOWN', uuid: 'UNKNOWN' },
device: deviceInfo = { model: 'UNKNOWN', uuid: 'UNKNOWN' },
} = params;
const device = await findOrCreate(token, { ...deviceInfo });
const locations = Array.isArray(list)
? list
: (
Expand Down
12 changes: 8 additions & 4 deletions src/server/models/Location.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ export async function getLatestLocation(params, isAdmin) {
return result;
}

export async function createLocation(location, deviceInfo, org) {
export async function createLocation(location, device, org) {
const now = new Date();
const device = await findOrCreate(org, { ...deviceInfo });

CompanyModel.update(
{ updated_at: now },
Expand All @@ -95,7 +94,7 @@ export async function createLocation(location, deviceInfo, org) {
'org:id'.green,
device.company_id,
'device:id'.green,
device.uuid,
device.device_id,
);
const row = {
latitude: location.coords.latitude,
Expand Down Expand Up @@ -154,8 +153,13 @@ export async function create(
const {
company_token: token = org || 'UNKNOWN',
location: list = [],
device = { model: 'UNKNOWN', uuid: 'UNKNOWN' },
device: deviceInfo = { model: 'UNKNOWN', uuid: 'UNKNOWN' },
} = params;
const device = await findOrCreate(
org,
{ device_id: deviceInfo.uuid },
);
console.log('deviceInfo', deviceInfo, device);
const locations = Array.isArray(list)
? list
: (
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/firebase-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ router.post('/locations', checkAuth(verify), async (req, res) => {
}

try {
await create(data);
await create(data, 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 @@ -168,7 +168,7 @@ router.post('/locations', getAuth(verify), async (req, res) => {
}

try {
await create(data);
await create(data, org);
return res.send({ success: true });
} catch (err) {
if (err instanceof AccessDeniedError) {
Expand Down Expand Up @@ -225,7 +225,6 @@ router.delete('/locations', checkAuth(verify), async (req, res) => {
);

res.send({ success: true });
res.status(500).send({ error: 'Something failed!' });
} catch (err) {
console.info('v1', 'delete /locations', JSON.stringify(req.query), err);
res.status(500).send({ error: 'Something failed!' });
Expand Down
24 changes: 24 additions & 0 deletions tests/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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,
coords: {
latitude: 45.519264,
longitude: -73.616931,
accuracy: 15.2,
speed: -1,
heading: -1,
altitude: 41.8,
},
activity: {
type: 'still',
confidence: 100,
},
battery: {
is_charging: true,
level: 0.92,
},
extras: { setCurrentPosition: true },
};

export const location2 = {
is_moving: false,
uuid: '03f4aa4c-ed00-4390-9e82-49f0c5799940',
timestamp: '2020-03-12T19:26:12.020Z',
Expand Down
10 changes: 10 additions & 0 deletions tests/firebase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ describe('firebase api', () => {
expect(res).to.be.json;
});

test('POST /locations/test', async () => {
const res = await chai
.request(server)
.post('/api/firebase/locations/test')
.set('Authorization', `Bearer ${token}`)
.send({ location });
expect(res).have.status(200);
expect(res).to.be.json;
});

test('DELETE /locations', async () => {
const res = await chai
.request(server)
Expand Down
29 changes: 25 additions & 4 deletions tests/site-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import chaiHttp from 'chai-http';

import {
location,
location2,
regData,
server,
} from './data';
Expand Down Expand Up @@ -82,7 +83,7 @@ describe('site api', () => {
.set('Authorization', `Bearer ${token}`)
.send({
location,
device: { model: 'test', uuid: 'test' },
device: { model: 'test', uuid: 'uuid' },
company_token: 'test',
});
expect(res).have.status(200);
Expand All @@ -96,7 +97,7 @@ describe('site api', () => {
.set('Authorization', `Bearer ${token}`)
.send([{
location,
device: { model: 'test', uuid: 'test' },
device: { model: 'test', uuid: 'uuid' },
company_token: 'test',
}]);
expect(res).have.status(200);
Expand All @@ -121,14 +122,34 @@ describe('site api', () => {
.post('/api/site/locations/test')
.set('Authorization', `Bearer ${token}`)
.send({
location,
device: { model: 'test', uuid: 'test' },
location: [location, location2],
device: { model: 'test', uuid: 'uuid' },
company_token: 'test',
});
expect(res).have.status(200);
expect(res).to.be.json;
});

test('POST /locations/test without device & company_token', async () => {
const res = await chai
.request(server)
.post('/api/site/locations/test')
.set('Authorization', `Bearer ${token}`)
.send({ location });
expect(res).have.status(200);
expect(res).to.be.json;
});

test('POST /locations/test [] without device & company_token', async () => {
const res = await chai
.request(server)
.post('/api/site/locations/test')
.set('Authorization', `Bearer ${token}`)
.send([{ location }]);
expect(res).have.status(200);
expect(res).to.be.json;
});

test('DELETE /locations', async () => {
const res = await chai.request(server)
.delete(
Expand Down

0 comments on commit 2a450cc

Please sign in to comment.