Skip to content

Commit b2dfe45

Browse files
paulbertlmmrssa
authored andcommitted
Replicate labels/tags with resources (fixes #2718) (#2728)
1 parent 049d438 commit b2dfe45

File tree

5 files changed

+61
-32
lines changed

5 files changed

+61
-32
lines changed

src/app/configuration/configuration.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export class ConfigurationService {
7777
// create replicator for pulling from parent at first as we do not have session
7878
this.syncService.sync({ ...replicatorObj, db: 'courses' }, credentials),
7979
this.syncService.sync({ ...replicatorObj, db: 'resources' }, credentials),
80+
this.syncService.sync({ ...replicatorObj, db: 'exams' }, credentials),
81+
this.syncService.sync({ ...replicatorObj, db: 'tags' }, credentials),
8082
this.syncService.sync(userReplicator, credentials)
8183
]);
8284
}

src/app/manager-dashboard/manager-dashboard.component.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,13 @@ export class ManagerDashboardComponent implements OnInit, OnDestroy {
198198
const removedItems = previousList.filter(item => selected.findIndex(i => i._id === item.id) < 0)
199199
.map(item => ({ ...item, sendOnAccept: false }));
200200
const dataUpdate = selected.map(item => ({ ...item, sendOnAccept: true })).concat(removedItems);
201-
if (db === 'courses') {
202-
this.handleCourseAttachments(selected, previousList);
201+
switch (db) {
202+
case 'courses':
203+
this.handleCourseAttachments(selected, previousList);
204+
break;
205+
case 'resources':
206+
this.handleResourceAttachments(selected);
207+
break;
203208
}
204209
this.couchService.post(db + '/_bulk_docs', { docs: dataUpdate }).subscribe(res => {
205210
this.planetMessageService.showMessage('Send on accept list updated');
@@ -216,6 +221,15 @@ export class ManagerDashboardComponent implements OnInit, OnDestroy {
216221
this.sendOnAcceptOkClick('exams', previousExams)(exams);
217222
}
218223

224+
handleResourceAttachments(resources) {
225+
const tagIds = [].concat.apply([], resources.map((resource: any) => resource.tags || []));
226+
if (tagIds.length > 0) {
227+
this.couchService.findAll('tags', findDocuments({ '_id': { '$in': tagIds } })).subscribe((tags) => {
228+
this.sendOnAcceptOkClick('tags', [])(tags);
229+
});
230+
}
231+
}
232+
219233
resetPin() {
220234
const userName = 'org.couchdb.user:satellite';
221235
this.couchService.get('_users/' + userName)

src/app/manager-dashboard/manager-fetch.component.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class ManagerFetchComponent implements OnInit, AfterViewInit {
7676

7777
getPushedItem() {
7878
const itemsToPull = this.selection.selected.map(id => findByIdInArray(this.pushedItems.data, id));
79-
const replicators = this.createRepicatorsArray(itemsToPull, []);
79+
const replicators = this.syncService.createRepicatorsArray(itemsToPull, 'pull', []);
8080
const deleteItems = itemsToPull.map(sentItem => ({ _id: sentItem._id, _rev: sentItem._rev, _deleted: true }));
8181
if (replicators.length > 0) {
8282
this.syncService.confirmPasswordAndRunReplicators(replicators).pipe(
@@ -87,31 +87,4 @@ export class ManagerFetchComponent implements OnInit, AfterViewInit {
8787
}
8888
}
8989

90-
createRepicatorsArray(itemsToPull, replicators = []) {
91-
return itemsToPull.reduce((newReplicators: any[], item: any) => {
92-
const pullItem = item.item;
93-
let pullObject = newReplicators.find((replicator: any) => replicator.db === item.db);
94-
if (!pullObject) {
95-
pullObject = { db: item.db, type: 'pull', date: true, items: [ pullItem ] };
96-
newReplicators.push(pullObject);
97-
} else {
98-
pullObject.items.push(pullItem);
99-
}
100-
if (item.db === 'courses') {
101-
return this.coursesItemsToPull(pullItem, newReplicators);
102-
}
103-
return newReplicators;
104-
}, replicators);
105-
}
106-
107-
coursesItemsToPull(course, replicators) {
108-
return this.createRepicatorsArray(
109-
[].concat.apply([], course.steps.map(step =>
110-
step.resources.map(r => ({ item: r, db: 'resources' }))
111-
.concat(step.exam ? [ { item: step.exam, db: 'exams' } ] : []))
112-
),
113-
replicators
114-
);
115-
}
116-
11790
}

src/app/resources/resources.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ export class ResourcesComponent implements OnInit, AfterViewInit, OnDestroy {
247247

248248
shareResource(type, resources) {
249249
const msg = (type === 'pull' ? 'fetch' : 'send'),
250-
items = resources.map(id => this.resources.data.find((resource: any) => resource._id === id));
251-
this.syncService.confirmPasswordAndRunReplicators([ { db: this.dbName, items, type: type, date: true } ])
250+
items = resources.map(id => ({ item: this.resources.data.find((resource: any) => resource._id === id), db: this.dbName }));
251+
this.syncService.confirmPasswordAndRunReplicators(this.syncService.createRepicatorsArray(items, type) )
252252
.subscribe((response: any) => {
253253
this.planetMessageService.showMessage(resources.length + ' ' + this.dbName + ' ' + 'queued to ' + msg);
254254
}, () => error => this.planetMessageService.showMessage(error));

src/app/shared/sync.service.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,44 @@ export class SyncService {
7373
};
7474
}
7575

76+
createRepicatorsArray(items, type: 'pull' | 'push', replicators = []) {
77+
return items.reduce((newReplicators: any[], item: any) => {
78+
const doc = item.item;
79+
let syncObject = newReplicators.find((replicator: any) => replicator.db === item.db);
80+
if (!syncObject) {
81+
syncObject = { db: item.db, type, date: true, items: [ doc ] };
82+
newReplicators.push(syncObject);
83+
} else {
84+
syncObject.items.push(doc);
85+
}
86+
switch (item.db) {
87+
case 'courses':
88+
return this.coursesItemsToSync(doc, type, newReplicators);
89+
case 'resources':
90+
return this.resourcesItemsToSync(doc, type, newReplicators);
91+
default:
92+
return newReplicators;
93+
}
94+
}, replicators);
95+
}
96+
97+
coursesItemsToSync(course, type, replicators) {
98+
return this.createRepicatorsArray(
99+
[].concat.apply([], course.steps.map(step =>
100+
step.resources.map(r => ({ item: r, db: 'resources' }))
101+
.concat(step.exam ? [ { item: step.exam, db: 'exams' } ] : []))
102+
),
103+
type,
104+
replicators
105+
);
106+
}
107+
108+
resourcesItemsToSync(resource, type, replicators) {
109+
return resource.tags === undefined ? replicators : this.createRepicatorsArray(
110+
resource.tags.map(tag => ({ item: { _id: tag }, db: 'tags' })),
111+
type,
112+
replicators
113+
);
114+
}
115+
76116
}

0 commit comments

Comments
 (0)