Skip to content

Commit

Permalink
fix migration script #67
Browse files Browse the repository at this point in the history
fix typo #67

review filters #67
  • Loading branch information
Sigura committed Mar 16, 2020
1 parent efa3c62 commit 9e1613c
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 83 deletions.
4 changes: 2 additions & 2 deletions bin/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ require('@babel/register')();

const {
// default: check,
transfer,
migrate,
} = require('../src/server/firebase/migration');

// check({ org: 'test', uuid: 'UNKNOWN' })
// .then(() => process.exit(1));

transfer()
migrate()
.then(() => process.exit(1));
2 changes: 1 addition & 1 deletion src/client/components/FilterView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const FilterView = ({
>
<RefreshIcon />
</IconButton>
<Typography variant='h6'>Filter!</Typography>
<Typography variant='h6'>Filter</Typography>
<IconButton edge='end' color='inherit' onClick={() => setOpen(false)}>
{theme.direction === 'ltr'
? <ChevronLeftIcon />
Expand Down
24 changes: 16 additions & 8 deletions src/client/reducer/dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ export const doAddTestMarker = (value: Object): AddTestMarkerAction => ({
// ------------------------------------

export const loadOrgTokens = (): ThunkAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const { dashboard: { orgTokenFromSearch }, auth } = getState();
const params = qs.stringify({ company_token: orgTokenFromSearch });
const { dashboard: { orgTokenFromSearch, company_token: orgToken }, auth } = getState();
const params = qs.stringify({ company_token: orgToken || orgTokenFromSearch });
try {
const headers = makeHeaders(auth);
const response = await fetch(`${API_URL}/company_tokens?${params}`, { headers });
Expand Down Expand Up @@ -428,7 +428,7 @@ export const loadLocations = (): ThunkAction => async (dispatch: Dispatch, getSt

const params = qs.stringify({
company_id: companyId,
company_token: orgToken,
company_token: orgToken || companyId,
device_id: deviceId,
end_date: endDate.toISOString(),
limit: maxMarkers,
Expand Down Expand Up @@ -456,7 +456,7 @@ export const loadCurrentLocation = (): ThunkAction => async (dispatch: Dispatch,
const params = qs.stringify({
device_id: deviceId,
company_id: companyId,
company_token: orgToken,
company_token: orgToken || companyId,
});
try {
const headers = makeHeaders(auth);
Expand Down Expand Up @@ -508,13 +508,20 @@ export const loadInitialData =

export const deleteActiveDevice =
(deleteOptions: ?DeleteOptions): ThunkAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const { dashboard: { deviceId, orgTokenFromSearch }, auth } = getState();
const {
dashboard: {
deviceId,
orgTokenFromSearch,
company_token: orgToken,
},
auth,
} = getState();
if (!deviceId) {
return;
}
const params = deleteOptions
? `?${qs.stringify({
company_token: orgTokenFromSearch,
company_token: orgToken || orgTokenFromSearch,
end_date: deleteOptions.endDate.toISOString(),
start_date: deleteOptions.startDate.toISOString(),
})}`
Expand Down Expand Up @@ -687,7 +694,7 @@ const autoselectOrInvalidateSelectedOrgTokenHandler = (
return cloneState(state, { companyId: `${orgTokens[0].id}` });
}
if (orgTokens.length > 1) {
const existingOrgToken = orgTokens && orgTokens.find((x: Device) => x.id === +companyId);
const existingOrgToken = orgTokens && orgTokens.find((x: Device) => `${x.id}` === `${companyId}`);
if (!existingOrgToken) {
return cloneState(state, { companyId: `${orgTokens[0].id}` });
}
Expand All @@ -700,14 +707,15 @@ const autoselectOrInvalidateSelectedDeviceHandler = (
state: DashboardState,
): DashboardState => {
const { devices, deviceId } = state;

if (devices.length === 0) {
return cloneState(state, { deviceId: null });
}
if (devices.length === 1) {
return cloneState(state, { deviceId: `${devices[0].id}` });
}
if (devices.length > 1) {
const existingDevice = devices && devices.find((x: Device) => x.id === +deviceId);
const existingDevice = devices && devices.find((x: Device) => `${x.id}` === `${deviceId}`);
if (!existingDevice) {
return cloneState(state, { deviceId: `${devices[0].id}` });
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const dummyToken = 'DUMMY_TOKEN';
export const isPostgres = !!process.env.DATABASE_URL;
export const desc = isPostgres ? 'DESC NULLS LAST' : 'DESC';
export const isProduction = process.env.NODE_ENV === 'production';
export const firebaseOperationLimit = !isProduction ? 500 : -1;
export const firebaseOperationLimit = 500;
export const firebaseURL = process.env.FIREBASE_URL;
export const firebasePrivateKey = (process.env.FIREBASE_PRIVATE_KEY || '').replace(/\\n/img, '\n');
export const firebaseProjectId = process.env.FIREBASE_PROJECT_ID;
Expand Down
2 changes: 1 addition & 1 deletion src/server/firebase/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function getDevice({ device_id: deviceId, org }) {

return result;
} catch (e) {
console.error('v3:getDevice', e);
console.error('v3:getDevice', org, deviceId, e);
return null;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/server/firebase/Location.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function createLocation(location, deviceInfo, org, batch) {
const currentDevice = await findOrCreate(org, { ...deviceInfo });

console.info(
'location:create'.green,
'v3:location:create'.green,
'org:name'.green,
org,
'org'.green,
Expand Down Expand Up @@ -147,7 +147,7 @@ export async function createLocations(
batch,
);
} catch (e) {
console.error('createLocation', e);
console.error('v3:createLocation', e);
throw e;
}
},
Expand All @@ -164,7 +164,7 @@ export async function create(params) {
try {
await create(pp);
} catch (e) {
console.error('create', e);
console.error('v3:create', e);
throw e;
}
},
Expand Down
26 changes: 23 additions & 3 deletions src/server/firebase/migration.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import { Op } from 'sequelize';
import Promise from 'bluebird';

Expand All @@ -17,7 +18,7 @@ import { firebaseOperationLimit } from '../config';
const checkLimit = async (counter, batch) => {
if (
firebaseOperationLimit > 0 &&
(counter + 1) > firebaseOperationLimit
(counter + 1) >= firebaseOperationLimit
) {
try {
await batch.commit();
Expand Down Expand Up @@ -56,8 +57,14 @@ export const transfer = async () => {
const response = await locsRef.get();
const locs = toRows(response);
const locations = await LocationModel.findAll({
where: { device_id: dev.id },
id: { [Op.notIn]: locs.map(loc => loc.id) },
where: {
device_id: dev.id,
id: {
[Op.notIn]: locs
.map(loc => +loc.id)
.filter(Boolean),
},
},
order: [['recorded_at', 'desc']],
raw: true,
});
Expand All @@ -72,6 +79,19 @@ export const transfer = async () => {
});
});
await batch.commit();
return counter < firebaseOperationLimit;
};

export const migrate = async () => {
const hasNoTail = await transfer();
if (!hasNoTail) {
try {
return migrate();
} catch (e) {
console.error('migrate', e);
}
}
return true;
};

export default async ({ uuid, org }) => {
Expand Down
5 changes: 4 additions & 1 deletion src/server/models/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ export async function deleteDevice(
org,
start_date: startDate,
},
isAdmin,
) {
const device = await DeviceModel.findOne({
where: { company_token: org, id: deviceId },
where: !isAdmin
? { company_token: org, id: deviceId }
: { id: deviceId },
attributes: [
'id',
'device_id',
Expand Down
6 changes: 3 additions & 3 deletions src/server/models/Location.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export async function createLocation(location, deviceInfo, org) {
);

console.info(
'location:create'.green,
'v1:location:create'.green,
'org:name'.green,
org,
'org:id'.green,
Expand Down Expand Up @@ -124,7 +124,7 @@ export async function createLocations(
org,
);
} catch (e) {
console.error('createLocation', e);
console.error('v1:createLocation', e);
throw e;
}
},
Expand All @@ -142,7 +142,7 @@ export async function create(
try {
await create(pp);
} catch (e) {
console.error('create', e);
console.error('v1:create', e);
throw e;
}
},
Expand Down
22 changes: 11 additions & 11 deletions src/server/routes/api-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ router.post('/register', async (req, res) => {

// eslint-disable-next-line no-console
console.info(
'POST /register '.green,
'v2: POST /register '.green,
'org'.green,
org,
'uuid'.green,
Expand Down Expand Up @@ -123,7 +123,7 @@ router.all('/refresh_token', checkAuth(verify), async (req, res) => {
};
// eslint-disable-next-line no-console
console.info(
'auth:refresh'.green,
'v2:auth:refresh'.green,
'org:name'.green,
org,
'device:id'.green,
Expand Down Expand Up @@ -188,7 +188,7 @@ router.delete('/devices/:id', checkAuth(verify), async (req, res) => {

// eslint-disable-next-line no-console
console.info(
'devices:delete'.green,
'v2:devices:delete'.green,
'device:id'.green,
id,
JSON.stringify(req.query),
Expand Down Expand Up @@ -228,7 +228,7 @@ router.get('/locations/latest', checkAuth(verify), async (req, res) => {
!companyId && ({ company_id: companyId } = await getDevice({ id: deviceId, org }) || {});
// eslint-disable-next-line no-console
console.info(
'locations:latest'.green,
'v2:locations:latest'.green,
'org:name'.green,
org,
companyId,
Expand Down Expand Up @@ -258,7 +258,7 @@ router.get('/locations', checkAuth(verify), async (req, res) => {
({ device_id: deviceId = deviceId } = req.query);
// eslint-disable-next-line no-console
console.info(
'locations:get'.green,
'v2:locations:get'.green,
'org:name'.green,
org,
'device:id'.green,
Expand Down Expand Up @@ -288,7 +288,7 @@ router.post('/locations', checkAuth(verify), async (req, res) => {
const { deviceId, org } = req.jwt;
// eslint-disable-next-line no-console
console.info(
'locations:post'.green,
'v2:locations:post'.green,
'org:name'.green,
org,
'device:id'.green,
Expand All @@ -304,7 +304,7 @@ router.post('/locations', checkAuth(verify), async (req, res) => {
if (!device) {
// eslint-disable-next-line no-console
console.error(
'Device ID %s not found. Was it deleted from dashboard?'.red,
'v2:Device ID %s not found. Was it deleted from dashboard?'.red,
deviceId,
);
return res.status(410).send({
Expand Down Expand Up @@ -345,7 +345,7 @@ router.post('/locations/:company_token', checkAuth(verify), async (req, res) =>

// eslint-disable-next-line no-console
console.info(
'locations:post'.green,
'v2:locations:post'.green,
'org:name'.green,
org,
'device:id'.green,
Expand All @@ -369,7 +369,7 @@ router.post('/locations/:company_token', checkAuth(verify), async (req, res) =>
return res.status(403).send({ error: err.toString() });
}
// eslint-disable-next-line no-console
console.error(`POST /locations/${org}`, err);
console.error(`v2: POST /locations/${org}`, err);
return res.status(500).send({ error: err.message });
}
});
Expand All @@ -382,7 +382,7 @@ router.delete('/locations', checkAuth(verify), async (req, res) => {

// eslint-disable-next-line no-console
console.info(
'locations:delete'.green,
'v2:locations:delete'.green,
'org:name'.green,
org,
'device:id'.green,
Expand All @@ -401,7 +401,7 @@ router.delete('/locations', checkAuth(verify), async (req, res) => {
res.send({ success: true });
} catch (err) {
// eslint-disable-next-line no-console
console.info('DELETE /locations', req.query, err);
console.info('v2: DELETE /locations', req.query, err);
res.status(500).send({ error: err.message });
}
});
Expand Down
Loading

0 comments on commit 9e1613c

Please sign in to comment.