UNPKG

@trimblemaps/content-db

Version:

An isomorphic indexeddb wrapper for storing/retrieving TrimbleMaps content (ie places & place-sets)

83 lines (75 loc) 2.69 kB
import { PlacesStore } from '../../src'; import { IPlaceSet } from '../../src/models'; const daClient = require('../utils/dataanywhere-client'); const auth = require('../utils/auth'); const { creds } = require('../utils/config'); const places$ = new PlacesStore('My Account Name'); // used to SETUP test, login to CT to simulate editing a place function modifyPlaceSetName() { // <--- 1 return places$.getAll() .then((sets: IPlaceSet[]) => { const customSets = sets.filter((set: IPlaceSet) => !set.AlkPublicReadOnly); const setId = customSets[0].Id; // first custom place-set return daClient.updateSetName(setId, `test_${new Date().getTime()}`); }); } function fetchAccountPlaces() { let placeSetResponse = null; return places$.getAll() .then(sets => { return { data: sets }; // wrapped response from db to mimic DA }) .catch(err => { if (err.type === 'NotFoundError') { return daClient.getPlaceSets(true) .then(res => { placeSetResponse = res; return places$.put(res.data) .then(() => placeSetResponse); // save placeSet to db and return }); // initial full fetch } console.error('fetchAccountPlaces()', err, '<<<'); }); } auth(creds) .then(res => { // console.log('auth res', res); daClient.setAuthHeader(`token=${res.authToken}`); return // modifyPlaceSetName(); // setup }) .then(() => { return fetchAccountPlaces(); }) .then(res => getModifiedSetIds(res.data)) .then(modifiedSetIds => { console.log('modifiedSetIds:', modifiedSetIds); if (modifiedSetIds.length) { const upserts = modifiedSetIds.map(id => upsertPlaceSetById(id)); return Promise.all(upserts); } return; }) .catch(err => console.log('Error:', err.response || err, '<<< Auth Error')); function getModifiedSetIds(sets) { // <--- 2 return daClient.getPlaceSets(false) // fresh fetch of sets .then(res => { const newSets = res.data.filter((set: IPlaceSet) => !set.AlkPublicReadOnly); const setIds = sets.map(s => s.Id); let modifiedSetIds = newSets.map(newSet => places$.findModifiedSetId(sets, newSet)); const addedSetIds = newSets.map(ns => ns.Id).filter(x => !setIds.includes(x)); if (addedSetIds.length) { console.log('new sets:', addedSetIds); modifiedSetIds = modifiedSetIds.concat(addedSetIds); } return modifiedSetIds.filter(a => a); }); } function upsertPlaceSetById(setId: number) { let placeSet; return daClient.getPlacesBySetId(setId) .then(res => { placeSet = res.data[0]; return places$.putPlaceSet(res.data[0]) }) .then(() => places$.updatePlaceSetIndex(setId, placeSet)); }