@coorpacademy/app-review 0.2.6 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ import { Rank } from '../types/common';
2
+ export declare const fetchRank: (token: string) => Promise<Rank>;
@@ -0,0 +1,10 @@
1
+ import crossFetch from 'cross-fetch';
2
+ import decode from 'jwt-decode';
3
+ import { toJSON } from './tools/fetch-responses';
4
+ export const fetchRank = async (token) => {
5
+ const { user: userId, host } = decode(token);
6
+ const response = await crossFetch(`${host}/api/v1/leaderboard/rank/${userId}?group=ALL`, {
7
+ headers: { authorization: token }
8
+ });
9
+ return toJSON(response);
10
+ };
@@ -1,10 +1,12 @@
1
1
  import { fetchCorrection } from './fetch-correction';
2
+ import { fetchRank } from './fetch-rank';
2
3
  import { fetchSkills } from './fetch-skills';
3
4
  import { fetchSlide } from './fetch-slide';
4
5
  import { postAnswer } from './post-answer';
5
6
  import { postProgression } from './post-progression';
6
7
  export const getServices = () => ({
7
8
  fetchCorrection,
9
+ fetchRank,
8
10
  fetchSkills,
9
11
  fetchSlide,
10
12
  postAnswer,
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,24 @@
1
+ import test from 'ava';
2
+ import nock from 'nock';
3
+ import { fetchRank } from '../fetch-rank';
4
+ const result = {
5
+ rank: 93
6
+ };
7
+ test.before(() => {
8
+ nock('http://localhost:3000')
9
+ .get('/api/v1/leaderboard/rank/592d830b240b923f00bffba6?group=ALL')
10
+ .reply(200, result);
11
+ });
12
+ test.after(() => {
13
+ nock.cleanAll();
14
+ });
15
+ test('should fetch rank with success', async (t) => {
16
+ const token = process.env.API_TEST_TOKEN || '';
17
+ const rank = await fetchRank(token);
18
+ t.deepEqual(result, rank);
19
+ });
20
+ test('should reject bad token', async (t) => {
21
+ const badToken = 'token is not a jwt';
22
+ const error = await t.throwsAsync(() => fetchRank(badToken));
23
+ t.is(error?.message, "Invalid token specified: Cannot read properties of undefined (reading 'replace')");
24
+ });
@@ -42,6 +42,9 @@ const fetchSkillsResponse = [
42
42
  custom: true
43
43
  }
44
44
  ];
45
+ const fetchRankResponse = {
46
+ rank: 93
47
+ };
45
48
  export const postProgressionResponse = {
46
49
  _id: '62b1d1087aa12f00253f40ee',
47
50
  content,
@@ -402,5 +405,6 @@ export const services = {
402
405
  const currentSlide = progression.state.nextContent.ref;
403
406
  return Promise.resolve(get(currentSlide, postAnswerResponses));
404
407
  },
405
- fetchCorrection: ref => Promise.resolve(getChoicesCorrection(ref))
408
+ fetchCorrection: ref => Promise.resolve(getChoicesCorrection(ref)),
409
+ fetchRank: () => Promise.resolve(fetchRankResponse)
406
410
  };
@@ -74,6 +74,9 @@ export declare type SlideFromAPI = {
74
74
  type: 'chapter' | 'course';
75
75
  };
76
76
  };
77
+ export declare type Rank = {
78
+ rank: number;
79
+ };
77
80
  export declare type ProgressionAnswerItem = {
78
81
  isCorrect: boolean;
79
82
  slideRef: string;
@@ -130,6 +133,7 @@ export declare type Services = {
130
133
  postProgression(skillRef: string, token: string): Promise<ProgressionFromAPI>;
131
134
  postAnswer(progression: ProgressionFromAPI, token: string, answer: string[]): Promise<ProgressionFromAPI>;
132
135
  fetchCorrection(slideRef: string, token: string, progressionId: string, answer: string[]): Promise<CorrectionFromAPI | void>;
136
+ fetchRank(token: string): Promise<Rank>;
133
137
  };
134
138
  export declare type Options = {
135
139
  services: Services;
@@ -0,0 +1,2 @@
1
+ import { Rank } from '../types/common';
2
+ export declare const fetchRank: (token: string) => Promise<Rank>;
@@ -0,0 +1,54 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (_) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import crossFetch from 'cross-fetch';
38
+ import decode from 'jwt-decode';
39
+ import { toJSON } from './tools/fetch-responses';
40
+ export var fetchRank = function (token) { return __awaiter(void 0, void 0, void 0, function () {
41
+ var _a, userId, host, response;
42
+ return __generator(this, function (_b) {
43
+ switch (_b.label) {
44
+ case 0:
45
+ _a = decode(token), userId = _a.user, host = _a.host;
46
+ return [4 /*yield*/, crossFetch("".concat(host, "/api/v1/leaderboard/rank/").concat(userId, "?group=ALL"), {
47
+ headers: { authorization: token }
48
+ })];
49
+ case 1:
50
+ response = _b.sent();
51
+ return [2 /*return*/, toJSON(response)];
52
+ }
53
+ });
54
+ }); };
@@ -1,10 +1,12 @@
1
1
  import { fetchCorrection } from './fetch-correction';
2
+ import { fetchRank } from './fetch-rank';
2
3
  import { fetchSkills } from './fetch-skills';
3
4
  import { fetchSlide } from './fetch-slide';
4
5
  import { postAnswer } from './post-answer';
5
6
  import { postProgression } from './post-progression';
6
7
  export var getServices = function () { return ({
7
8
  fetchCorrection: fetchCorrection,
9
+ fetchRank: fetchRank,
8
10
  fetchSkills: fetchSkills,
9
11
  fetchSlide: fetchSlide,
10
12
  postAnswer: postAnswer,
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,78 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (_) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import test from 'ava';
38
+ import nock from 'nock';
39
+ import { fetchRank } from '../fetch-rank';
40
+ var result = {
41
+ rank: 93
42
+ };
43
+ test.before(function () {
44
+ nock('http://localhost:3000')
45
+ .get('/api/v1/leaderboard/rank/592d830b240b923f00bffba6?group=ALL')
46
+ .reply(200, result);
47
+ });
48
+ test.after(function () {
49
+ nock.cleanAll();
50
+ });
51
+ test('should fetch rank with success', function (t) { return __awaiter(void 0, void 0, void 0, function () {
52
+ var token, rank;
53
+ return __generator(this, function (_a) {
54
+ switch (_a.label) {
55
+ case 0:
56
+ token = process.env.API_TEST_TOKEN || '';
57
+ return [4 /*yield*/, fetchRank(token)];
58
+ case 1:
59
+ rank = _a.sent();
60
+ t.deepEqual(result, rank);
61
+ return [2 /*return*/];
62
+ }
63
+ });
64
+ }); });
65
+ test('should reject bad token', function (t) { return __awaiter(void 0, void 0, void 0, function () {
66
+ var badToken, error;
67
+ return __generator(this, function (_a) {
68
+ switch (_a.label) {
69
+ case 0:
70
+ badToken = 'token is not a jwt';
71
+ return [4 /*yield*/, t.throwsAsync(function () { return fetchRank(badToken); })];
72
+ case 1:
73
+ error = _a.sent();
74
+ t.is(error === null || error === void 0 ? void 0 : error.message, "Invalid token specified: Cannot read properties of undefined (reading 'replace')");
75
+ return [2 /*return*/];
76
+ }
77
+ });
78
+ }); });
@@ -54,6 +54,9 @@ var fetchSkillsResponse = [
54
54
  custom: true
55
55
  }
56
56
  ];
57
+ var fetchRankResponse = {
58
+ rank: 93
59
+ };
57
60
  export var postProgressionResponse = {
58
61
  _id: '62b1d1087aa12f00253f40ee',
59
62
  content: content,
@@ -415,5 +418,6 @@ export var services = {
415
418
  var currentSlide = progression.state.nextContent.ref;
416
419
  return Promise.resolve(get(currentSlide, postAnswerResponses));
417
420
  },
418
- fetchCorrection: function (ref) { return Promise.resolve(getChoicesCorrection(ref)); }
421
+ fetchCorrection: function (ref) { return Promise.resolve(getChoicesCorrection(ref)); },
422
+ fetchRank: function () { return Promise.resolve(fetchRankResponse); }
419
423
  };
@@ -74,6 +74,9 @@ export declare type SlideFromAPI = {
74
74
  type: 'chapter' | 'course';
75
75
  };
76
76
  };
77
+ export declare type Rank = {
78
+ rank: number;
79
+ };
77
80
  export declare type ProgressionAnswerItem = {
78
81
  isCorrect: boolean;
79
82
  slideRef: string;
@@ -130,6 +133,7 @@ export declare type Services = {
130
133
  postProgression(skillRef: string, token: string): Promise<ProgressionFromAPI>;
131
134
  postAnswer(progression: ProgressionFromAPI, token: string, answer: string[]): Promise<ProgressionFromAPI>;
132
135
  fetchCorrection(slideRef: string, token: string, progressionId: string, answer: string[]): Promise<CorrectionFromAPI | void>;
136
+ fetchRank(token: string): Promise<Rank>;
133
137
  };
134
138
  export declare type Options = {
135
139
  services: Services;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coorpacademy/app-review",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "description": "",
5
5
  "engines": {
6
6
  "node": ">=16.15.0"
@@ -41,7 +41,7 @@
41
41
  "./package.json": "./package.json"
42
42
  },
43
43
  "dependencies": {
44
- "@coorpacademy/components": "10.22.21",
44
+ "@coorpacademy/components": "10.22.23",
45
45
  "@coorpacademy/redux-task": "^1.1.5",
46
46
  "cross-fetch": "^3.1.5",
47
47
  "jwt-decode": "^3.1.2",
@@ -78,5 +78,5 @@
78
78
  "ts-node": "^10.9.1",
79
79
  "typescript": "^4.7.4"
80
80
  },
81
- "gitHead": "2496c794545cc1b5177f07e585d4f3740aa6f7b6"
81
+ "gitHead": "31b40916b16bd71f9e2717ef10fdb00f4404f5c4"
82
82
  }
@@ -0,0 +1,14 @@
1
+ import crossFetch from 'cross-fetch';
2
+ import decode from 'jwt-decode';
3
+
4
+ import {JWT, Rank} from '../types/common';
5
+ import {toJSON} from './tools/fetch-responses';
6
+
7
+ export const fetchRank = async (token: string): Promise<Rank> => {
8
+ const {user: userId, host}: JWT = decode(token);
9
+ const response = await crossFetch(`${host}/api/v1/leaderboard/rank/${userId}?group=ALL`, {
10
+ headers: {authorization: token}
11
+ });
12
+
13
+ return toJSON<Rank>(response);
14
+ };
@@ -1,5 +1,6 @@
1
1
  import {Services} from '../types/common';
2
2
  import {fetchCorrection} from './fetch-correction';
3
+ import {fetchRank} from './fetch-rank';
3
4
  import {fetchSkills} from './fetch-skills';
4
5
  import {fetchSlide} from './fetch-slide';
5
6
  import {postAnswer} from './post-answer';
@@ -7,6 +8,7 @@ import {postProgression} from './post-progression';
7
8
 
8
9
  export const getServices = (): Services => ({
9
10
  fetchCorrection,
11
+ fetchRank,
10
12
  fetchSkills,
11
13
  fetchSlide,
12
14
  postAnswer,
@@ -0,0 +1,34 @@
1
+ import test from 'ava';
2
+ import nock from 'nock';
3
+ import {Rank} from '../../types/common';
4
+ import {fetchRank} from '../fetch-rank';
5
+
6
+ const result: Rank = {
7
+ rank: 93
8
+ };
9
+
10
+ test.before(() => {
11
+ nock('http://localhost:3000')
12
+ .get('/api/v1/leaderboard/rank/592d830b240b923f00bffba6?group=ALL')
13
+ .reply(200, result);
14
+ });
15
+
16
+ test.after(() => {
17
+ nock.cleanAll();
18
+ });
19
+
20
+ test('should fetch rank with success', async t => {
21
+ const token = process.env.API_TEST_TOKEN || '';
22
+ const rank = await fetchRank(token);
23
+ t.deepEqual(result, rank);
24
+ });
25
+
26
+ test('should reject bad token', async t => {
27
+ const badToken = 'token is not a jwt';
28
+ const error = await t.throwsAsync(() => fetchRank(badToken));
29
+
30
+ t.is(
31
+ error?.message,
32
+ "Invalid token specified: Cannot read properties of undefined (reading 'replace')"
33
+ );
34
+ });
@@ -55,6 +55,10 @@ const fetchSkillsResponse = [
55
55
  }
56
56
  ];
57
57
 
58
+ const fetchRankResponse = {
59
+ rank: 93
60
+ };
61
+
58
62
  export const postProgressionResponse: ProgressionFromAPI = {
59
63
  _id: '62b1d1087aa12f00253f40ee',
60
64
  content,
@@ -419,5 +423,6 @@ export const services: Services = {
419
423
  const currentSlide = progression.state.nextContent.ref;
420
424
  return Promise.resolve(get(currentSlide, postAnswerResponses));
421
425
  },
422
- fetchCorrection: ref => Promise.resolve(getChoicesCorrection(ref))
426
+ fetchCorrection: ref => Promise.resolve(getChoicesCorrection(ref)),
427
+ fetchRank: () => Promise.resolve(fetchRankResponse)
423
428
  };
@@ -90,6 +90,10 @@ export type SlideFromAPI = {
90
90
  };
91
91
  };
92
92
 
93
+ export type Rank = {
94
+ rank: number;
95
+ };
96
+
93
97
  export type ProgressionAnswerItem = {
94
98
  isCorrect: boolean;
95
99
  slideRef: string;
@@ -163,6 +167,7 @@ export type Services = {
163
167
  progressionId: string,
164
168
  answer: string[]
165
169
  ): Promise<CorrectionFromAPI | void>;
170
+ fetchRank(token: string): Promise<Rank>;
166
171
  };
167
172
 
168
173
  export type Options = {