Skip to content

Commit

Permalink
all or none
Browse files Browse the repository at this point in the history
  • Loading branch information
freddieptf committed Feb 18, 2025
1 parent f25f98b commit 69b5081
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
25 changes: 12 additions & 13 deletions src/routes/add-place.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,18 @@ export default async function addPlace(fastify: FastifyInstance) {
if (!place) {
throw Error(`unable to find place ${id}`);
}

const chtApi = new ChtApi(req.chtSession);
RemotePlaceCache.clear(chtApi, place.type.name);
await RemotePlaceResolver.resolveOne(place, sessionCache, chtApi, { fuzz: true });
place.validate();
let places = [];
if (place.hasSharedUser) {
places = sessionCache.getPlaces({ type: place.type.name }).filter(p => p.contact.id === place.contact.id);
} else {
places = [place];
}
await RemotePlaceResolver.resolve(places, sessionCache, chtApi, { fuzz: true });
places.forEach(place => place.validate());
await WarningSystem.setWarnings(place.type, chtApi, sessionCache);

fastify.uploadManager.triggerRefresh(place.id);
places.forEach(place => fastify.uploadManager.triggerRefresh(place.id));
});

fastify.post('/place/upload/:id', async (req) => {
Expand All @@ -158,16 +162,11 @@ export default async function addPlace(fastify: FastifyInstance) {
if (!place) {
throw Error(`unable to find place ${id}`);
}
const places = [];
let 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);
}
places = sessionCache.getPlaces({ type: place.type.name }).filter(p => p.contact.id === place.contact.id);
} else {
places.push(place);
places = [place];
}

const uploadManager: UploadManager = fastify.uploadManager;
Expand Down
10 changes: 8 additions & 2 deletions src/services/place-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export default class PlaceFactory {
place.setPropertiesFromFormData(placeData, 'hierarchy_');
if (idx > 0) {
place.contact = places[0].contact;
place.hasSharedUser = true;
}
place.hasSharedUser = true;
places.push(place);
});
await PlaceFactory.finalizePlaces(places, sessionCache, chtApi, contactType);
Expand All @@ -60,8 +60,14 @@ export default class PlaceFactory {
throw new Error('unknown place or place has already been created');
}
place.setPropertiesFromFormData(formData, 'hierarchy_');
let places = [];
if (place.hasSharedUser) {
places = sessionCache.getPlaces({ type: place.type.name }).filter(p => p.contact.id === place.contact.id);
} else {
places = [place];
}

await PlaceFactory.finalizePlaces([place], sessionCache, chtApi, place.type);
await PlaceFactory.finalizePlaces(places, sessionCache, chtApi, place.type);
return place;
};

Expand Down
12 changes: 7 additions & 5 deletions src/services/upload-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ export class UploadManager extends EventEmitter {

await this.uploadPlacesInBatches(independants, chtApi);
await this.uploadPlacesInBatches(dependants, chtApi);
await this.uploadGrouped(validPlaces, chtApi);
await this.uploadGrouped(validPlaces.filter(p => p.hasSharedUser), 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[0].creationDetails.username) {
let creationDetails = places.find(p => !!p.creationDetails.username)?.creationDetails;
if (!creationDetails) {
await this.uploadSinglePlace(places[0], api);
creationDetails = places[0].creationDetails;
}
await this.uploadGroup(places[0].creationDetails, places.slice(1), api);
await this.uploadGroup(creationDetails, places, api);
});
};

Expand Down Expand Up @@ -102,8 +104,8 @@ export class UploadManager extends EventEmitter {
}
const placeIds: {[key:string]: any} = { [creationDetails.placeId]: '' };
for (const place of places) {
if (place.isCreated && place.creationDetails.placeId) {
placeIds[place.creationDetails.placeId] = undefined;
if (place.isCreated) {
placeIds[place.id] = undefined;
continue;
}
this.eventedPlaceStateChange(place, PlaceUploadState.IN_PROGRESS);
Expand Down

0 comments on commit 69b5081

Please sign in to comment.