Skip to content

Commit

Permalink
upload individual
Browse files Browse the repository at this point in the history
  • Loading branch information
freddieptf committed Feb 17, 2025
1 parent 542d2c5 commit 48ab5ac
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 26 deletions.
16 changes: 14 additions & 2 deletions src/routes/add-place.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,26 @@ export default async function addPlace(fastify: FastifyInstance) {
fastify.post('/place/upload/:id', async (req) => {
const { id } = req.params as any;
const sessionCache: SessionCache = req.sessionCache;
const chtApi = new ChtApi(req.chtSession);

const place = sessionCache.getPlace(id);
if (!place) {
throw Error(`unable to find place ${id}`);
}
const places = [];
if (place.hasSharedUser) {
const group = sessionCache.getPlaces({ type: place.type.name }).filter(p => p.contact.id === place.contact.id);
if (group.filter(p => p.isCreated).length > 0) {
places.push(...group.filter(p => p.isCreated), place);
} else {
places.push(group[0], place);
}
} else {
places.push(place);
}

const chtApi = new ChtApi(req.chtSession);
const uploadManager: UploadManager = fastify.uploadManager;
uploadManager.doUpload([place], chtApi, true);
uploadManager.doUpload(places, chtApi, true);
});

fastify.post('/place/remove/:id', async (req) => {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default async function sessionCache(fastify: FastifyInstance) {
const directiveModel = new DirectiveModel(sessionCache, req.cookies.filter);

const chtApi = new ChtApi(req.chtSession);
uploadManager.doUpload(sessionCache.getPlaces(), chtApi, true);
uploadManager.doUpload(sessionCache.getPlaces(), chtApi, ignoreWarnings === 'true');

return resp.view('src/liquid/place/directive.html', {
directiveModel
Expand Down
2 changes: 1 addition & 1 deletion src/routes/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default async function events(fastify: FastifyInstance) {
};

uploadManager.on('refresh_table_row', placeChangeListener);
uploadManager.on('refresh_grouped', async (arg: string) => {
uploadManager.on('refresh_grouped', async () => {
resp.sse({ event: `update-group`, data: `update` });
});

Expand Down
72 changes: 51 additions & 21 deletions src/services/upload-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,28 @@ export interface Uploader {

export class UploadManager extends EventEmitter {
doUpload = async (places: Place[], chtApi: ChtApi, ignoreWarnings: boolean = false) => {
const placesNeedingUpload = places.filter(p => {
return !p.isCreated && !p.hasValidationErrors && (ignoreWarnings || !p.warnings.length);
const validPlaces = places.filter(p => {
return !p.hasValidationErrors && (ignoreWarnings || !p.warnings.length);
});
const placesNeedingUpload = validPlaces.filter(p => !p.isCreated);
this.eventedPlaceStateChange(placesNeedingUpload, PlaceUploadState.SCHEDULED);

const independants = placesNeedingUpload.filter(p => !p.isDependant && !p.hasSharedUser);
const dependants = placesNeedingUpload.filter(p => p.isDependant && !p.hasSharedUser);

await this.uploadPlacesInBatches(independants, chtApi);
await this.uploadPlacesInBatches(dependants, chtApi);
await this.uploadGrouped(placesNeedingUpload, chtApi);
await this.uploadGrouped(validPlaces, chtApi);
};

uploadGrouped = async (places: Place[], api: ChtApi) => {
const grouped = _.groupBy(places, place => place.contact.id);
Object.keys(grouped).forEach(async k => {
const places = grouped[k];
if (places.length > 1) {
await this.uploadGroup(places[0].creationDetails, places.slice(1), api);
if (!places[0].creationDetails.username) {
await this.uploadSinglePlace(places[0], api);
}
await this.uploadGroup(places[0].creationDetails, places.slice(1), api);
});
};

Expand Down Expand Up @@ -98,29 +100,57 @@ export class UploadManager extends EventEmitter {
if (!creationDetails.username || !creationDetails.placeId) {
throw new Error('creationDetails must not be empty');
}
const placeIds: {[key:string]: string} = { [creationDetails.placeId]: '' };
const placeIds: {[key:string]: any} = { [creationDetails.placeId]: '' };
for (const place of places) {
if (place.isCreated && place.creationDetails.placeId) {
placeIds[place.creationDetails.placeId] = undefined;
continue;
}
this.eventedPlaceStateChange(place, PlaceUploadState.IN_PROGRESS);
const payload = place.asChtPayload(api.chtSession.username, creationDetails.contactId);
await Config.mutate(payload, api, place.isReplacement);
const result = await api.createPlace(payload);

place.creationDetails.contactId = result.contactId;
place.creationDetails.placeId = result.placeId;
placeIds[result.placeId] = result.placeId;
try {
const payload = place.asChtPayload(api.chtSession.username, creationDetails.contactId);
await Config.mutate(payload, api, place.isReplacement);

const result = await api.createPlace(payload);

place.creationDetails.contactId = result.contactId;
place.creationDetails.placeId = result.placeId;
placeIds[result.placeId] = undefined;

} catch (err) {
const errorDetails = getErrorDetails(err);
console.log('error when creating place', errorDetails);
place.uploadError = errorDetails;
this.eventedPlaceStateChange(place, PlaceUploadState.FAILURE);
}
}

await api.updateUser({ username: creationDetails.username!, place: Object.keys(placeIds)});
const created_at = new Date().getTime();
places.forEach(place => {
place.creationDetails.username = creationDetails.username;
place.creationDetails.password = creationDetails.password;
place.creationDetails.created_at = created_at;
try {
await api.updateUser({ username: creationDetails.username, place: Object.keys(placeIds)});
const created_at = new Date().getTime();

this.eventedPlaceStateChange(place, PlaceUploadState.SUCCESS);
});
places.forEach(place => {
place.creationDetails.username = creationDetails.username;
place.creationDetails.password = creationDetails.password;
place.creationDetails.created_at = created_at;
this.eventedPlaceStateChange(place, PlaceUploadState.SUCCESS);
});

this.emit('refresh_grouped', creationDetails.contactId);

this.emit('refresh_grouped', creationDetails.contactId);
} catch (err) {
const errorDetails = getErrorDetails(err);
console.log('error when creating user', errorDetails);

places.forEach(place => {
place.uploadError = errorDetails;
this.eventedPlaceStateChange(place, PlaceUploadState.FAILURE);
});

this.emit('refresh_grouped', creationDetails.contactId);
}

}

public triggerRefresh(place_id: string) {
Expand Down
2 changes: 1 addition & 1 deletion test/services/upload-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ describe('services/upload-manager.ts', () => {
expect(chu.creationDetails).to.deep.include({
contactId: 'created-contact-id',
placeId: 'created-place-id',
username: 'new'
username: 'new_cha'
});
expect(chu.creationDetails.password).to.not.be.undefined;

Expand Down

0 comments on commit 48ab5ac

Please sign in to comment.