Skip to content

Commit 4bcc8d9

Browse files
authored
feat(API): Add contextActivities property to fullActivities collection (LLC-293) (#686)
1 parent 3ca24bc commit 4bcc8d9

File tree

18 files changed

+498
-163
lines changed

18 files changed

+498
-163
lines changed

src/apps/statements/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ export default (appConfig: AppConfig): Result => {
7373
repo,
7474
tracker: appConfig.tracker,
7575
});
76-
const presenter = presenterFactory({
76+
77+
return presenterFactory({
7778
allowFormBody: appConfig.presenter.express.allowFormBody,
7879
allowUndefinedMethod: appConfig.presenter.express.allowUndefinedMethod,
7980
bodyParserLimit: appConfig.presenter.express.bodyParserLimit,
@@ -86,5 +87,4 @@ export default (appConfig: AppConfig): Result => {
8687
tracker: appConfig.tracker,
8788
translator,
8889
});
89-
return presenter;
9090
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import Extensions from './Extensions';
22
import LanguageMap from './LanguageMap';
33

4-
interface FullActivityModel {
4+
interface FullActivity {
55
readonly activityId: string;
6+
readonly organisationId: string;
7+
readonly lrsId: string;
68
readonly name: LanguageMap;
79
readonly description: LanguageMap;
810
readonly moreInfo?: string;
911
readonly type?: string;
1012
readonly extensions: Extensions;
11-
readonly organisationId: string;
12-
readonly lrsId: string;
1313
}
1414

15-
export default FullActivityModel;
15+
export default FullActivity;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Context from './Context';
2+
import FullActivity from './FullActivity';
3+
4+
interface FullActivityClient extends FullActivity {
5+
readonly context?: Context;
6+
}
7+
8+
export default FullActivityClient;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import FullActivityContextActivities from './FullActivityContextActivities';
2+
3+
interface FullActivityContext {
4+
readonly contextActivities?: FullActivityContextActivities;
5+
}
6+
7+
export default FullActivityContext;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
interface FullActivityContextActivities {
2+
readonly parent?: string[];
3+
readonly grouping?: string[];
4+
readonly category?: string[];
5+
readonly other?: string[];
6+
}
7+
8+
export default FullActivityContextActivities;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import FullActivity from './FullActivity';
2+
import FullActivityContext from './FullActivityContext';
3+
4+
interface FullActivityDatabase extends FullActivity {
5+
readonly context?: FullActivityContext;
6+
}
7+
8+
export default FullActivityDatabase;

src/apps/statements/repo/modelsRepo/getFullActivity/Signature.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Member from 'jscommons/dist/utils/Member';
22
import ClientModel from '../../../models/ClientModel';
33
import Extensions from '../../../models/Extensions';
4+
import FullActivityContext from '../../../models/FullActivityContext';
45
import LanguageMap from '../../../models/LanguageMap';
56

67
export interface Opts {
@@ -15,6 +16,7 @@ export interface Result {
1516
readonly extensions: Extensions;
1617
readonly type?: string;
1718
readonly moreInfo?: string;
19+
readonly context?: FullActivityContext;
1820
}
1921

2022
type Signature = Member<Opts, Result>;

src/apps/statements/repo/modelsRepo/getFullActivity/mongo.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default (config: FacadeConfig): Signature => {
1919
extensions: 1,
2020
moreInfo: 1,
2121
type: 1,
22+
context: 1,
2223
};
2324

2425
const result = await collection.findOne(query, { fields });
@@ -37,8 +38,19 @@ export default (config: FacadeConfig): Signature => {
3738
name: result.name,
3839
description: result.description,
3940
extensions: replaceDotsInExtensions(/&46;/g, '.')(result.extensions),
40-
...(result.type === undefined ? {} : { type: result.type }),
41-
...(result.moreInfo === undefined ? {} : { moreInfo: result.moreInfo }),
41+
...(
42+
result.type === undefined
43+
? {}
44+
: { type: result.type }),
45+
...(
46+
result.moreInfo === undefined
47+
? {}
48+
: { moreInfo: result.moreInfo }),
49+
...(
50+
result.context === undefined
51+
? {}
52+
: { context: result.context }
53+
),
4254
};
4355
};
4456
};

src/apps/statements/repo/modelsRepo/updateFullActivities/Signature.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
import Member from 'jscommons/dist/utils/Member';
2-
import ClientModel from '../../../models/ClientModel';
3-
import LanguageMap from '../../../models/LanguageMap';
4-
5-
export interface Update {
6-
readonly activityId: string;
7-
readonly name: LanguageMap;
8-
readonly description: LanguageMap;
9-
readonly extensions: LanguageMap;
10-
readonly moreInfo?: string;
11-
readonly type?: string;
12-
}
2+
import FullActivityDatabase from '../../../models/FullActivityDatabase';
133

144
export interface Opts {
15-
readonly updates: Update[];
16-
readonly client: ClientModel;
5+
readonly fullActivities: FullActivityDatabase[];
176
}
187

198
type Signature = Member<Opts, void>;

src/apps/statements/repo/modelsRepo/updateFullActivities/mongo.ts

Lines changed: 93 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,107 @@
11
import { Dictionary, mapKeys } from 'lodash';
2-
import { ObjectID } from 'mongodb';
2+
import { isEmpty } from 'lodash';
3+
import { ObjectID, UnorderedBulkOperation } from 'mongodb';
4+
import FullActivityDatabase from '../../../models/FullActivityDatabase';
35
import { FULL_ACTIVITIES_COLLECTION_NAME } from '../utils/mongoModels/constants';
46
import FacadeConfig from '../utils/mongoModels/FacadeConfig';
57
import matchesFullActivity from '../utils/mongoModels/matchesFullActivity';
68
import { replaceDotsInExtensions } from '../utils/mongoModels/replaceDotsInStatement';
79
import Signature from './Signature';
810

9-
const getPatchUpdate = <T>(patch: Dictionary<T>, parentKeys: string[]) => {
10-
return mapKeys<T>(patch as any, (_value: T, key: number) => {
11-
const parentPath = parentKeys.join('.');
12-
return `${parentPath}.${key}`;
11+
const getPatchUpdate = <T>(patch: Dictionary<T>, parentKeys: string[]) =>
12+
mapKeys<T>(
13+
patch as any,
14+
(_value: T, key: number) => `${parentKeys.join('.')}.${key}`,
15+
);
16+
17+
const createMongoAddToSet = (fullActivity: FullActivityDatabase) => (
18+
{
19+
...(fullActivity.context?.contextActivities?.parent !== undefined
20+
? {
21+
'context.contextActivities.parent': {
22+
$each: fullActivity.context.contextActivities.parent,
23+
},
24+
}
25+
: {}
26+
),
27+
...(fullActivity.context?.contextActivities?.category !== undefined
28+
? {
29+
'context.contextActivities.category': {
30+
$each: fullActivity.context.contextActivities.category,
31+
},
32+
}
33+
: {}
34+
),
35+
...(fullActivity.context?.contextActivities?.other !== undefined
36+
? {
37+
'context.contextActivities.other': {
38+
$each: fullActivity.context.contextActivities.other,
39+
},
40+
}
41+
: {}
42+
),
43+
...(fullActivity.context?.contextActivities?.grouping !== undefined
44+
? {
45+
'context.contextActivities.grouping': {
46+
$each: fullActivity.context.contextActivities.grouping,
47+
},
48+
}
49+
: {}
50+
),
51+
}
52+
);
53+
54+
const createMongoSet = (fullActivity: FullActivityDatabase) => {
55+
const extensions = replaceDotsInExtensions(/\./g, '&46;')(fullActivity.extensions);
56+
57+
return {
58+
...getPatchUpdate(fullActivity.name, ['name']),
59+
...getPatchUpdate(fullActivity.description, ['description']),
60+
...getPatchUpdate(extensions, ['extensions']),
61+
...(
62+
fullActivity.moreInfo !== undefined
63+
? { moreInfo: fullActivity.moreInfo }
64+
: {}
65+
),
66+
...(
67+
fullActivity.type !== undefined
68+
? { type: fullActivity.type }
69+
: {}
70+
),
71+
};
72+
};
73+
74+
const createMongoQuery = (fullActivity: FullActivityDatabase) => {
75+
const activityId = fullActivity.activityId;
76+
const lrsId = new ObjectID(fullActivity.lrsId);
77+
const organisationId = new ObjectID(fullActivity.organisationId);
78+
79+
return matchesFullActivity({ activityId, lrsId, organisationId });
80+
};
81+
82+
const creatBatchQuery = (batch: UnorderedBulkOperation) => (fullActivity: FullActivityDatabase) => {
83+
const mongoQuery = createMongoQuery(fullActivity);
84+
// tslint:disable:no-inferred-empty-object-type
85+
const mongoSet = createMongoSet(fullActivity);
86+
const mongoAddToSet = createMongoAddToSet(fullActivity);
87+
// tslint:enable:no-inferred-empty-object-type
88+
89+
batch.find(mongoQuery).upsert().updateOne({
90+
...(!isEmpty(mongoSet) ? { $set: mongoSet } : {}),
91+
...(!isEmpty(mongoAddToSet) ? { $addToSet: mongoAddToSet } : {}),
1392
});
1493
};
1594

16-
export default (config: FacadeConfig): Signature => {
17-
return async ({ client, updates }) => {
95+
export default (config: FacadeConfig): Signature =>
96+
async ({ fullActivities }) => {
97+
if (fullActivities.length < 1) {
98+
return;
99+
}
100+
18101
const collection = (await config.db()).collection(FULL_ACTIVITIES_COLLECTION_NAME);
19-
const lrsId = new ObjectID(client.lrs_id);
20-
const organisationId = new ObjectID(client.organisation);
21102
const batch = collection.initializeUnorderedBulkOp();
22103

23-
updates.forEach((update) => {
24-
const activityId = update.activityId;
25-
const extensions = replaceDotsInExtensions(/\./g, '&46;')(update.extensions);
26-
const mongoQuery = matchesFullActivity({ activityId, lrsId, organisationId });
27-
const mongoSet = {
28-
...getPatchUpdate(update.name, ['name']),
29-
...getPatchUpdate(update.description, ['description']),
30-
...getPatchUpdate(extensions, ['extensions']),
31-
...(update.moreInfo !== undefined ? { moreInfo: update.moreInfo } : {}),
32-
...(update.type !== undefined ? { type: update.type } : {}),
33-
};
34-
if (Object.keys(mongoSet).length === 0) {
35-
/* istanbul ignore next */
36-
return;
37-
}
38-
const mongoUpdate = { $set: mongoSet };
39-
40-
batch.find(mongoQuery).upsert().updateOne(mongoUpdate);
41-
});
42-
43-
if (updates.length > 0) {
44-
await batch.execute();
45-
}
104+
fullActivities.forEach(creatBatchQuery(batch));
105+
await batch.execute();
106+
// tslint:disable-next-line:max-file-line-count
46107
};
47-
};

0 commit comments

Comments
 (0)