@quesmed/types 2.1.7 → 2.1.10

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.
@@ -1,5 +1,6 @@
1
1
  export * as admin from './admin';
2
2
  export * as restricted from './restricted';
3
+ export * from './sample';
3
4
  export * from './stripe';
4
5
  export * from './users';
5
6
  export * as validUserToken from './validUserToken';
@@ -25,6 +25,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
25
25
  exports.validUserToken = exports.restricted = exports.admin = void 0;
26
26
  exports.admin = __importStar(require("./admin"));
27
27
  exports.restricted = __importStar(require("./restricted"));
28
+ __exportStar(require("./sample"), exports);
28
29
  __exportStar(require("./stripe"), exports);
29
30
  __exportStar(require("./users"), exports);
30
31
  exports.validUserToken = __importStar(require("./validUserToken"));
@@ -1,3 +1,5 @@
1
+ import { ApolloCache, ApolloClient } from '@apollo/client';
2
+ import { ApolloUpdateOptions, ApolloUpdateResultRestricted } from '../..';
1
3
  import { Id } from '../../../models';
2
4
  import { graphqlNormalize, RestrictedData } from '../../types';
3
5
  import { IMarksheet } from './../../../models/Marksheet';
@@ -29,6 +31,8 @@ export interface IMarksheetInput {
29
31
  timeTaken: number;
30
32
  }
31
33
  export declare const SAVE_MARKSHEET: import("@apollo/client").DocumentNode;
34
+ export declare const updateMarksheets: (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<ISaveMarksheetsData>, options: ApolloUpdateOptions) => void;
35
+ export declare const optimisticSaveMarkseehts: (client: ApolloClient<any>, marksheetInput: IMarksheetInput, questionIndex: number) => ISaveMarksheetsData;
32
36
  export interface ISaveMarksheetsVar {
33
37
  marksheetInput: IMarksheetInput[];
34
38
  }
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.REMOVE_FLAGGED_QUESTION = exports.ADD_FLAGGED_QUESTION = exports.SAVE_MARKSHEET = exports.RE_BUILD_MARKSHEET = exports.BUILD_MARKSHEET = void 0;
3
+ exports.REMOVE_FLAGGED_QUESTION = exports.ADD_FLAGGED_QUESTION = exports.optimisticSaveMarkseehts = exports.updateMarksheets = exports.SAVE_MARKSHEET = exports.RE_BUILD_MARKSHEET = exports.BUILD_MARKSHEET = void 0;
4
4
  const client_1 = require("@apollo/client");
5
5
  const marksheet_1 = require("../../fragments/marksheet");
6
+ const restricted_1 = require("../../query/restricted");
6
7
  exports.BUILD_MARKSHEET = (0, client_1.gql) `
7
8
  mutation BuildMarksheet($buildMarksheet: BuildMarksheetInput) {
8
9
  restricted {
@@ -49,14 +50,6 @@ exports.SAVE_MARKSHEET = (0, client_1.gql) `
49
50
  createdAt
50
51
  startedAt
51
52
  endedAt
52
- topicIds
53
- topicNames
54
- source
55
- userId
56
- user {
57
- displayName
58
- id
59
- }
60
53
  marks {
61
54
  id
62
55
  marksheetId
@@ -65,15 +58,81 @@ exports.SAVE_MARKSHEET = (0, client_1.gql) `
65
58
  flagged
66
59
  mark
67
60
  }
68
- mockTestId
69
61
  correct
70
62
  incorrect
71
- totalQuestions
72
- isTestMarksheet
73
63
  }
74
64
  }
75
65
  }
76
66
  `;
67
+ const updateMarksheets = (cache, result, options) => {
68
+ const { saveMarksheets } = result?.data?.restricted || {};
69
+ const { variables } = options || {};
70
+ if (!options || !variables || !saveMarksheets) {
71
+ return;
72
+ }
73
+ try {
74
+ for (let i = 0; i < saveMarksheets.length; i++) {
75
+ const { marksheetInput } = variables;
76
+ const marksheet = marksheetInput[i];
77
+ cache.writeFragment({
78
+ id: cache.identify({
79
+ id: saveMarksheets[i],
80
+ __typename: 'MarksheetMark',
81
+ }),
82
+ data: {
83
+ timeTaken: marksheet.timeTaken || 0,
84
+ mark: marksheet.mark,
85
+ questionChoiceId: marksheet.choiceId,
86
+ },
87
+ fragment: (0, client_1.gql) `
88
+ fragment NewTodoMark on MarksheetMark {
89
+ timeTaken
90
+ questionChoiceId
91
+ mark
92
+ }
93
+ `,
94
+ });
95
+ }
96
+ }
97
+ catch (error) {
98
+ console.error(error);
99
+ }
100
+ };
101
+ exports.updateMarksheets = updateMarksheets;
102
+ const optimisticSaveMarkseehts = (client, marksheetInput, questionIndex) => {
103
+ const { timeTaken, choiceId, mark, marksheetId } = marksheetInput;
104
+ const data = client.readQuery({
105
+ variables: { id: marksheetId },
106
+ query: restricted_1.MARKSHEET,
107
+ });
108
+ const { marksheet } = data?.restricted || {};
109
+ if (marksheet) {
110
+ const { marks = [], ...rest } = marksheet || {};
111
+ const updatedMark = {
112
+ ...marks[questionIndex],
113
+ marksheetId,
114
+ timeTaken,
115
+ questionChoiceId: choiceId || null,
116
+ mark: mark || null,
117
+ };
118
+ const udatedMarks = [
119
+ ...marks.slice(0, questionIndex),
120
+ updatedMark,
121
+ ...marks.slice(questionIndex + 1),
122
+ ];
123
+ return {
124
+ restricted: {
125
+ saveMarksheets: [{ ...rest, marks: udatedMarks }],
126
+ },
127
+ };
128
+ }
129
+ return {
130
+ restricted: {
131
+ saveMarksheets: [],
132
+ },
133
+ };
134
+ };
135
+ exports.optimisticSaveMarkseehts = optimisticSaveMarkseehts;
77
136
  exports.ADD_FLAGGED_QUESTION = (0, client_1.gql) `
78
137
  mutation AddFlaggedQuestion($markId: Int!, $questionId: Int!) {
79
138
  restricted {
@@ -1,12 +1,27 @@
1
- import { EQuestionLike, Id, IQuestion, IQuestionComment } from '../../../models';
1
+ import { ApolloCache, ApolloClient } from '@apollo/client';
2
+ import { ApolloUpdateOptions, ApolloUpdateResultRestricted } from '../..';
3
+ import { EQuestionLike, EQuestionType, Id, IQuestion, IQuestionComment } from '../../../models';
2
4
  import { graphqlNormalize, RestrictedData } from '../../types';
5
+ interface LikeData {
6
+ likes?: number;
7
+ dislikes?: number;
8
+ isLikedByMe?: EQuestionLike;
9
+ }
10
+ export declare const getLikeData: (like: EQuestionLike, item: LikeData) => LikeData;
11
+ export declare const getQuestionTypeName: (typeId: EQuestionType) => string;
3
12
  export declare const QUESTION_LIKE: import("@apollo/client").DocumentNode;
13
+ export declare const optimisticQuestionLike: (marksheetId: number, client: ApolloClient<any>, input: IQuestionLikeVar) => IQuestionLikeData;
4
14
  export interface IQuestionLikeVar {
5
15
  questionId: Id;
6
16
  like: EQuestionLike;
7
17
  }
8
18
  export declare type IQuestionLikeData = RestrictedData<graphqlNormalize & IQuestion, 'questionLike'>;
9
19
  export declare const QUESTION_COMMENTS: import("@apollo/client").DocumentNode;
20
+ export declare const updateQuestionComments: (marksheetId: number) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentsData>, options: ApolloUpdateOptions) => void;
21
+ export declare const optimisticQuestionComment: (id: number, user: {
22
+ id: number;
23
+ displayName: string;
24
+ }, input: IQuestionCommentsVar) => IQuestionCommentsData;
10
25
  export interface IQuestionCommentsVar {
11
26
  questionId: Id;
12
27
  parentId?: Id;
@@ -14,13 +29,17 @@ export interface IQuestionCommentsVar {
14
29
  }
15
30
  export declare type IQuestionCommentsData = RestrictedData<graphqlNormalize & IQuestionComment, 'questionComments'>;
16
31
  export declare const QUESTION_COMMENT_LIKE: import("@apollo/client").DocumentNode;
32
+ export declare const optimisticCommentLike: (comments: IQuestionComment[], input: IQuestionCommentsLikeVar) => IQuestionCommentsLikeData;
17
33
  export interface IQuestionCommentsLikeVar {
18
34
  commentId: Id;
19
35
  like: EQuestionLike;
20
36
  }
21
37
  export declare type IQuestionCommentsLikeData = RestrictedData<graphqlNormalize & IQuestionComment, 'questionCommentLike'>;
22
38
  export declare const QUESTION_COMMENT_REMOVE: import("@apollo/client").DocumentNode;
39
+ export declare const updateQuestionCommentsRemove: (marksheetId: number) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentRemoveData>, options: ApolloUpdateOptions) => void;
40
+ export declare const optimisticQuestionCommentRemove: (comments: IQuestionComment[], input: IQuestionCommentRemoveVar, parentId?: number | undefined) => IQuestionCommentRemoveData;
23
41
  export interface IQuestionCommentRemoveVar {
24
42
  commentId: Id;
25
43
  }
26
44
  export declare type IQuestionCommentRemoveData = RestrictedData<graphqlNormalize & IQuestionComment, 'questionCommentRemove'>;
45
+ export {};
@@ -1,45 +1,86 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.QUESTION_COMMENT_REMOVE = exports.QUESTION_COMMENT_LIKE = exports.QUESTION_COMMENTS = exports.QUESTION_LIKE = void 0;
3
+ exports.optimisticQuestionCommentRemove = exports.updateQuestionCommentsRemove = exports.QUESTION_COMMENT_REMOVE = exports.optimisticCommentLike = exports.QUESTION_COMMENT_LIKE = exports.optimisticQuestionComment = exports.updateQuestionComments = exports.QUESTION_COMMENTS = exports.optimisticQuestionLike = exports.QUESTION_LIKE = exports.getQuestionTypeName = exports.getLikeData = void 0;
4
4
  const client_1 = require("@apollo/client");
5
+ const models_1 = require("../../../models");
6
+ const restricted_1 = require("../../query/restricted");
7
+ const getLikeData = (like, item) => {
8
+ let { likes = 0, dislikes = 0, isLikedByMe } = item;
9
+ if (isLikedByMe === models_1.EQuestionLike.LIKE) {
10
+ likes = likes - 1;
11
+ if (like === models_1.EQuestionLike.DISLIKE) {
12
+ dislikes = dislikes + 1;
13
+ }
14
+ }
15
+ else if (isLikedByMe === models_1.EQuestionLike.DISLIKE) {
16
+ dislikes = dislikes - 1;
17
+ if (like === models_1.EQuestionLike.LIKE) {
18
+ likes = likes + 1;
19
+ }
20
+ }
21
+ else {
22
+ if (like === models_1.EQuestionLike.LIKE) {
23
+ likes = likes + 1;
24
+ }
25
+ else if (like === models_1.EQuestionLike.DISLIKE) {
26
+ dislikes = dislikes + 1;
27
+ }
28
+ }
29
+ return { likes, dislikes, isLikedByMe: like };
30
+ };
31
+ exports.getLikeData = getLikeData;
32
+ const getQuestionTypeName = (typeId) => {
33
+ switch (typeId) {
34
+ case models_1.EQuestionType.QUESTION_ANSWER:
35
+ return 'QuestionQA';
36
+ case models_1.EQuestionType.PRESCRIPTION_ANSWER:
37
+ return 'QuestionPrescription';
38
+ case models_1.EQuestionType.MULTIPLE_ANSWERS:
39
+ return 'QuestionMultiA';
40
+ case models_1.EQuestionType.SINGLE_BEST_ANSWER:
41
+ default:
42
+ return 'QuestionSBA';
43
+ }
44
+ };
45
+ exports.getQuestionTypeName = getQuestionTypeName;
5
46
  exports.QUESTION_LIKE = (0, client_1.gql) `
6
47
  mutation QuestionLike($questionId: Int!, $like: Int!) {
7
48
  restricted {
8
49
  questionLike(questionId: $questionId, like: $like) {
9
50
  id
10
51
  likes
52
+ typeId
11
53
  dislikes
12
54
  isLikedByMe
13
- comments {
14
- id
15
- createdAt
16
- comment
17
- likes
18
- user {
19
- id
20
- displayName
21
- }
22
- dislikes
23
- isLikedByMe
24
- questionId
25
- replies {
26
- id
27
- createdAt
28
- comment
29
- user {
30
- id
31
- displayName
32
- }
33
- likes
34
- dislikes
35
- isLikedByMe
36
- questionId
37
- }
38
- }
39
55
  }
40
56
  }
41
57
  }
42
58
  `;
59
+ const optimisticQuestionLike = (marksheetId, client, input) => {
60
+ const { questionId, like } = input;
61
+ const data = client.readQuery({
62
+ variables: { id: marksheetId },
63
+ query: restricted_1.MARKSHEET,
64
+ });
65
+ const { marksheet } = data?.restricted || {};
66
+ const { marks = [] } = marksheet || {};
67
+ const { question } = marks.find(({ question }) => Number(question.id) === Number(questionId)) ||
68
+ {};
69
+ if (question) {
70
+ const { typeId } = question;
71
+ return {
72
+ restricted: {
73
+ questionLike: {
74
+ ...question,
75
+ __typename: (0, exports.getQuestionTypeName)(typeId),
76
+ ...(0, exports.getLikeData)(like, question),
77
+ },
78
+ },
79
+ };
80
+ }
81
+ return {};
82
+ };
83
+ exports.optimisticQuestionLike = optimisticQuestionLike;
43
84
  exports.QUESTION_COMMENTS = (0, client_1.gql) `
44
85
  mutation QuestionComments(
45
86
  $questionId: Int!
@@ -55,11 +96,12 @@ exports.QUESTION_COMMENTS = (0, client_1.gql) `
55
96
  id
56
97
  createdAt
57
98
  comment
58
- likes
99
+ parentId
59
100
  user {
60
101
  id
61
102
  displayName
62
103
  }
104
+ likes
63
105
  dislikes
64
106
  isLikedByMe
65
107
  questionId
@@ -67,6 +109,7 @@ exports.QUESTION_COMMENTS = (0, client_1.gql) `
67
109
  id
68
110
  createdAt
69
111
  comment
112
+ parentId
70
113
  user {
71
114
  id
72
115
  displayName
@@ -80,6 +123,90 @@ exports.QUESTION_COMMENTS = (0, client_1.gql) `
80
123
  }
81
124
  }
82
125
  `;
126
+ const updateQuestionComments = (marksheetId) => (cache, result, options) => {
127
+ const { questionComments } = result?.data?.restricted || {};
128
+ const { variables } = options || {};
129
+ if (!variables || !questionComments) {
130
+ return;
131
+ }
132
+ const { questionId, parentId } = variables;
133
+ try {
134
+ const prevData = cache.readQuery({
135
+ variables: { id: marksheetId },
136
+ query: restricted_1.MARKSHEET,
137
+ });
138
+ if (prevData) {
139
+ const { marksheet } = prevData.restricted || {};
140
+ const { marks, ...marksheetRest } = marksheet;
141
+ const index = marks.findIndex(({ question }) => Number(question.id) === Number(questionId));
142
+ const { question, ...markRest } = marks[index];
143
+ const { comments = [], ...questionRest } = question;
144
+ let newComments;
145
+ if (parentId) {
146
+ const commentIndex = comments.findIndex(({ id }) => Number(id) === parentId);
147
+ const { replies = [], ...commentRest } = comments[commentIndex];
148
+ newComments = [
149
+ ...comments.slice(0, commentIndex),
150
+ { ...commentRest, replies: [...replies, questionComments] },
151
+ ...comments.slice(commentIndex + 1),
152
+ ];
153
+ }
154
+ else {
155
+ newComments = [...comments, questionComments];
156
+ }
157
+ cache.writeQuery({
158
+ query: restricted_1.MARKSHEET,
159
+ variables: { id: marksheetId },
160
+ data: {
161
+ ...prevData,
162
+ restricted: {
163
+ ...prevData.restricted,
164
+ marksheet: {
165
+ ...marksheetRest,
166
+ marks: [
167
+ ...marks.slice(0, index),
168
+ {
169
+ ...markRest,
170
+ question: { ...questionRest, comments: newComments },
171
+ },
172
+ ...marks.slice(index + 1),
173
+ ],
174
+ },
175
+ },
176
+ },
177
+ });
178
+ }
179
+ }
180
+ catch (error) {
181
+ console.error(error);
182
+ }
183
+ };
184
+ exports.updateQuestionComments = updateQuestionComments;
185
+ const optimisticQuestionComment = (id, user, input) => {
186
+ const { parentId, questionId, comment } = input;
187
+ const { displayName, id: userId } = user || {};
188
+ const newComment = {
189
+ createdAt: new Date(),
190
+ id,
191
+ likes: 0,
192
+ questionId,
193
+ dislikes: 0,
194
+ isLikedByMe: models_1.EQuestionLike.NONE,
195
+ user: { displayName, id: userId },
196
+ comment,
197
+ parentId: parentId ? parentId : 0,
198
+ replies: [],
199
+ };
200
+ return {
201
+ restricted: {
202
+ questionComments: {
203
+ __typename: 'QuestionComments',
204
+ ...newComment,
205
+ },
206
+ },
207
+ };
208
+ };
209
+ exports.optimisticQuestionComment = optimisticQuestionComment;
83
210
  exports.QUESTION_COMMENT_LIKE = (0, client_1.gql) `
84
211
  mutation QuestionCommentLike($commentId: Int!, $like: Int!) {
85
212
  restricted {
@@ -87,11 +214,12 @@ exports.QUESTION_COMMENT_LIKE = (0, client_1.gql) `
87
214
  id
88
215
  createdAt
89
216
  comment
90
- likes
217
+ parentId
91
218
  user {
92
219
  id
93
220
  displayName
94
221
  }
222
+ likes
95
223
  dislikes
96
224
  isLikedByMe
97
225
  questionId
@@ -99,6 +227,7 @@ exports.QUESTION_COMMENT_LIKE = (0, client_1.gql) `
99
227
  id
100
228
  createdAt
101
229
  comment
230
+ parentId
102
231
  user {
103
232
  id
104
233
  displayName
@@ -112,6 +241,22 @@ exports.QUESTION_COMMENT_LIKE = (0, client_1.gql) `
112
241
  }
113
242
  }
114
243
  `;
244
+ const optimisticCommentLike = (comments, input) => {
245
+ const { commentId, like } = input;
246
+ const comment = comments.find(({ id }) => Number(id) === Number(commentId)) ||
247
+ {};
248
+ return {
249
+ restricted: {
250
+ questionCommentLike: {
251
+ ...comment,
252
+ parentId: comment.parentId ? comment.parentId : 0,
253
+ __typename: 'QuestionComment',
254
+ ...(0, exports.getLikeData)(like, comment),
255
+ },
256
+ },
257
+ };
258
+ };
259
+ exports.optimisticCommentLike = optimisticCommentLike;
115
260
  exports.QUESTION_COMMENT_REMOVE = (0, client_1.gql) `
116
261
  mutation QuestionCommentRemove($commentId: Int!) {
117
262
  restricted {
@@ -119,11 +264,12 @@ exports.QUESTION_COMMENT_REMOVE = (0, client_1.gql) `
119
264
  id
120
265
  createdAt
121
266
  comment
122
- likes
267
+ parentId
123
268
  user {
124
269
  id
125
270
  displayName
126
271
  }
272
+ likes
127
273
  dislikes
128
274
  isLikedByMe
129
275
  questionId
@@ -131,6 +277,7 @@ exports.QUESTION_COMMENT_REMOVE = (0, client_1.gql) `
131
277
  id
132
278
  createdAt
133
279
  comment
280
+ parentId
134
281
  user {
135
282
  id
136
283
  displayName
@@ -144,3 +291,92 @@ exports.QUESTION_COMMENT_REMOVE = (0, client_1.gql) `
144
291
  }
145
292
  }
146
293
  `;
294
+ const updateQuestionCommentsRemove = (marksheetId) => (cache, result, options) => {
295
+ const { questionCommentRemove } = result?.data?.restricted || {};
296
+ const { variables } = options || {};
297
+ if (!variables || !questionCommentRemove) {
298
+ return;
299
+ }
300
+ const { questionId, id: commentId, parentId } = questionCommentRemove;
301
+ try {
302
+ const prevData = cache.readQuery({
303
+ variables: { id: marksheetId },
304
+ query: restricted_1.MARKSHEET,
305
+ });
306
+ if (prevData) {
307
+ const { marksheet } = prevData.restricted || {};
308
+ const { marks, ...marksheetRest } = marksheet;
309
+ const index = marks.findIndex(({ question }) => Number(question.id) === Number(questionId));
310
+ const { question, ...markRest } = marks[index];
311
+ const { comments = [], ...questionRest } = question;
312
+ let newComments;
313
+ if (parentId) {
314
+ const commentIndex = comments.findIndex(({ id }) => Number(id) === Number(parentId));
315
+ const { replies = [], ...commentRest } = comments[commentIndex];
316
+ newComments = [
317
+ ...comments.slice(0, commentIndex),
318
+ {
319
+ ...commentRest,
320
+ replies: replies.filter(({ id }) => Number(id) !== Number(commentId)),
321
+ },
322
+ ...comments.slice(commentIndex + 1),
323
+ ];
324
+ }
325
+ else {
326
+ newComments = comments.filter(({ id }) => Number(id) !== Number(commentId));
327
+ }
328
+ cache.writeQuery({
329
+ query: restricted_1.MARKSHEET,
330
+ variables: { id: marksheetId },
331
+ data: {
332
+ ...prevData,
333
+ restricted: {
334
+ ...prevData.restricted,
335
+ marksheet: {
336
+ ...marksheetRest,
337
+ marks: [
338
+ ...marks.slice(0, index),
339
+ {
340
+ ...markRest,
341
+ question: { ...questionRest, comments: newComments },
342
+ },
343
+ ...marks.slice(index + 1),
344
+ ],
345
+ },
346
+ },
347
+ },
348
+ });
349
+ }
350
+ }
351
+ catch (error) {
352
+ console.error(error);
353
+ }
354
+ };
355
+ exports.updateQuestionCommentsRemove = updateQuestionCommentsRemove;
356
+ const optimisticQuestionCommentRemove = (comments, input, parentId) => {
357
+ const { commentId } = input;
358
+ let comment;
359
+ if (parentId) {
360
+ const parent = comments.find(({ id }) => Number(id) === Number(parentId)) ||
361
+ {};
362
+ comment =
363
+ parent?.replies?.find(({ id }) => Number(id) === Number(commentId)) ||
364
+ {};
365
+ }
366
+ else {
367
+ comment =
368
+ comments.find(({ id }) => Number(id) === Number(commentId)) ||
369
+ {};
370
+ }
371
+ return {
372
+ restricted: {
373
+ questionCommentRemove: {
374
+ __typename: 'QuestionComments',
375
+ ...comment,
376
+ parentId: parentId ? parentId : 0,
377
+ replies: comment.replies ? comment.replies : [],
378
+ },
379
+ },
380
+ };
381
+ };
382
+ exports.optimisticQuestionCommentRemove = optimisticQuestionCommentRemove;
@@ -0,0 +1,3 @@
1
+ import { ApolloClient } from '@apollo/client';
2
+ import { IMarksheetInput } from './restricted';
3
+ export declare const saveSampleMarkseehts: (client: ApolloClient<any>, marksheetInput: IMarksheetInput, questionIndex: number) => void;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.saveSampleMarkseehts = void 0;
4
+ const query_1 = require("../query");
5
+ const saveSampleMarkseehts = (client, marksheetInput, questionIndex) => {
6
+ const { timeTaken, choiceId, mark, marksheetId } = marksheetInput;
7
+ const prevData = client.readQuery({
8
+ variables: { marksheetId },
9
+ query: query_1.SAMPLE_MARKSHEET,
10
+ });
11
+ if (prevData) {
12
+ const { sampleMarksheet } = prevData;
13
+ const { marks = [], ...rest } = sampleMarksheet || {};
14
+ client.writeQuery({
15
+ query: query_1.SAMPLE_MARKSHEET,
16
+ variables: { marksheetId },
17
+ data: {
18
+ ...prevData,
19
+ sampleMarksheet: {
20
+ ...rest,
21
+ marks: [
22
+ ...marks.slice(0, questionIndex),
23
+ {
24
+ ...marks[questionIndex],
25
+ timeTaken,
26
+ questionChoiceId: choiceId || null,
27
+ mark: mark || null,
28
+ },
29
+ ...marks.slice(questionIndex + 1),
30
+ ],
31
+ },
32
+ },
33
+ });
34
+ }
35
+ };
36
+ exports.saveSampleMarkseehts = saveSampleMarkseehts;
@@ -8,14 +8,18 @@ exports.MARKSHEET = (0, client_1.gql) `
8
8
  marksheet(id: $id) {
9
9
  id
10
10
  createdAt
11
+ startedAt
11
12
  endedAt
12
13
  isTestMarksheet
13
14
  source
15
+ correct
16
+ incorrect
14
17
  marks {
15
18
  id
16
19
  flagged
17
20
  index
18
21
  questionChoiceId
22
+ marksheetId
19
23
  timeTaken
20
24
  mark
21
25
  question {
@@ -1,5 +1,6 @@
1
1
  export * as admin from './admin';
2
2
  export * as restricted from './restricted';
3
+ export * from './sample';
3
4
  export * from './stripe';
4
5
  export * from './users';
5
6
  export * as validUserToken from './validUserToken';
@@ -1,5 +1,6 @@
1
1
  export * as admin from './admin';
2
2
  export * as restricted from './restricted';
3
+ export * from './sample';
3
4
  export * from './stripe';
4
5
  export * from './users';
5
6
  export * as validUserToken from './validUserToken';
@@ -1,3 +1,5 @@
1
+ import { ApolloCache, ApolloClient } from '@apollo/client';
2
+ import { ApolloUpdateOptions, ApolloUpdateResultRestricted } from '../..';
1
3
  import { Id } from '../../../models';
2
4
  import { graphqlNormalize, RestrictedData } from '../../types';
3
5
  import { IMarksheet } from './../../../models/Marksheet';
@@ -29,6 +31,8 @@ export interface IMarksheetInput {
29
31
  timeTaken: number;
30
32
  }
31
33
  export declare const SAVE_MARKSHEET: import("@apollo/client").DocumentNode;
34
+ export declare const updateMarksheets: (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<ISaveMarksheetsData>, options: ApolloUpdateOptions) => void;
35
+ export declare const optimisticSaveMarkseehts: (client: ApolloClient<any>, marksheetInput: IMarksheetInput, questionIndex: number) => ISaveMarksheetsData;
32
36
  export interface ISaveMarksheetsVar {
33
37
  marksheetInput: IMarksheetInput[];
34
38
  }
@@ -1,5 +1,6 @@
1
1
  import { gql } from '@apollo/client';
2
2
  import { MARKSHEET_MARK_FIELDS } from '../../fragments/marksheet';
3
+ import { MARKSHEET, } from '../../query/restricted';
3
4
  export const BUILD_MARKSHEET = gql `
4
5
  mutation BuildMarksheet($buildMarksheet: BuildMarksheetInput) {
5
6
  restricted {
@@ -46,14 +47,6 @@ export const SAVE_MARKSHEET = gql `
46
47
  createdAt
47
48
  startedAt
48
49
  endedAt
49
- topicIds
50
- topicNames
51
- source
52
- userId
53
- user {
54
- displayName
55
- id
56
- }
57
50
  marks {
58
51
  id
59
52
  marksheetId
@@ -62,15 +55,79 @@ export const SAVE_MARKSHEET = gql `
62
55
  flagged
63
56
  mark
64
57
  }
65
- mockTestId
66
58
  correct
67
59
  incorrect
68
- totalQuestions
69
- isTestMarksheet
70
60
  }
71
61
  }
72
62
  }
73
63
  `;
64
+ export const updateMarksheets = (cache, result, options) => {
65
+ const { saveMarksheets } = result?.data?.restricted || {};
66
+ const { variables } = options || {};
67
+ if (!options || !variables || !saveMarksheets) {
68
+ return;
69
+ }
70
+ try {
71
+ for (let i = 0; i < saveMarksheets.length; i++) {
72
+ const { marksheetInput } = variables;
73
+ const marksheet = marksheetInput[i];
74
+ cache.writeFragment({
75
+ id: cache.identify({
76
+ id: saveMarksheets[i],
77
+ __typename: 'MarksheetMark',
78
+ }),
79
+ data: {
80
+ timeTaken: marksheet.timeTaken || 0,
81
+ mark: marksheet.mark,
82
+ questionChoiceId: marksheet.choiceId,
83
+ },
84
+ fragment: gql `
85
+ fragment NewTodoMark on MarksheetMark {
86
+ timeTaken
87
+ questionChoiceId
88
+ mark
89
+ }
90
+ `,
91
+ });
92
+ }
93
+ }
94
+ catch (error) {
95
+ console.error(error);
96
+ }
97
+ };
98
+ export const optimisticSaveMarkseehts = (client, marksheetInput, questionIndex) => {
99
+ const { timeTaken, choiceId, mark, marksheetId } = marksheetInput;
100
+ const data = client.readQuery({
101
+ variables: { id: marksheetId },
102
+ query: MARKSHEET,
103
+ });
104
+ const { marksheet } = data?.restricted || {};
105
+ if (marksheet) {
106
+ const { marks = [], ...rest } = marksheet || {};
107
+ const updatedMark = {
108
+ ...marks[questionIndex],
109
+ marksheetId,
110
+ timeTaken,
111
+ questionChoiceId: choiceId || null,
112
+ mark: mark || null,
113
+ };
114
+ const udatedMarks = [
115
+ ...marks.slice(0, questionIndex),
116
+ updatedMark,
117
+ ...marks.slice(questionIndex + 1),
118
+ ];
119
+ return {
120
+ restricted: {
121
+ saveMarksheets: [{ ...rest, marks: udatedMarks }],
122
+ },
123
+ };
124
+ }
125
+ return {
126
+ restricted: {
127
+ saveMarksheets: [],
128
+ },
129
+ };
130
+ };
74
131
  export const ADD_FLAGGED_QUESTION = gql `
75
132
  mutation AddFlaggedQuestion($markId: Int!, $questionId: Int!) {
76
133
  restricted {
@@ -1,12 +1,27 @@
1
- import { EQuestionLike, Id, IQuestion, IQuestionComment } from '../../../models';
1
+ import { ApolloCache, ApolloClient } from '@apollo/client';
2
+ import { ApolloUpdateOptions, ApolloUpdateResultRestricted } from '../..';
3
+ import { EQuestionLike, EQuestionType, Id, IQuestion, IQuestionComment } from '../../../models';
2
4
  import { graphqlNormalize, RestrictedData } from '../../types';
5
+ interface LikeData {
6
+ likes?: number;
7
+ dislikes?: number;
8
+ isLikedByMe?: EQuestionLike;
9
+ }
10
+ export declare const getLikeData: (like: EQuestionLike, item: LikeData) => LikeData;
11
+ export declare const getQuestionTypeName: (typeId: EQuestionType) => string;
3
12
  export declare const QUESTION_LIKE: import("@apollo/client").DocumentNode;
13
+ export declare const optimisticQuestionLike: (marksheetId: number, client: ApolloClient<any>, input: IQuestionLikeVar) => IQuestionLikeData;
4
14
  export interface IQuestionLikeVar {
5
15
  questionId: Id;
6
16
  like: EQuestionLike;
7
17
  }
8
18
  export declare type IQuestionLikeData = RestrictedData<graphqlNormalize & IQuestion, 'questionLike'>;
9
19
  export declare const QUESTION_COMMENTS: import("@apollo/client").DocumentNode;
20
+ export declare const updateQuestionComments: (marksheetId: number) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentsData>, options: ApolloUpdateOptions) => void;
21
+ export declare const optimisticQuestionComment: (id: number, user: {
22
+ id: number;
23
+ displayName: string;
24
+ }, input: IQuestionCommentsVar) => IQuestionCommentsData;
10
25
  export interface IQuestionCommentsVar {
11
26
  questionId: Id;
12
27
  parentId?: Id;
@@ -14,13 +29,17 @@ export interface IQuestionCommentsVar {
14
29
  }
15
30
  export declare type IQuestionCommentsData = RestrictedData<graphqlNormalize & IQuestionComment, 'questionComments'>;
16
31
  export declare const QUESTION_COMMENT_LIKE: import("@apollo/client").DocumentNode;
32
+ export declare const optimisticCommentLike: (comments: IQuestionComment[], input: IQuestionCommentsLikeVar) => IQuestionCommentsLikeData;
17
33
  export interface IQuestionCommentsLikeVar {
18
34
  commentId: Id;
19
35
  like: EQuestionLike;
20
36
  }
21
37
  export declare type IQuestionCommentsLikeData = RestrictedData<graphqlNormalize & IQuestionComment, 'questionCommentLike'>;
22
38
  export declare const QUESTION_COMMENT_REMOVE: import("@apollo/client").DocumentNode;
39
+ export declare const updateQuestionCommentsRemove: (marksheetId: number) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentRemoveData>, options: ApolloUpdateOptions) => void;
40
+ export declare const optimisticQuestionCommentRemove: (comments: IQuestionComment[], input: IQuestionCommentRemoveVar, parentId?: number | undefined) => IQuestionCommentRemoveData;
23
41
  export interface IQuestionCommentRemoveVar {
24
42
  commentId: Id;
25
43
  }
26
44
  export declare type IQuestionCommentRemoveData = RestrictedData<graphqlNormalize & IQuestionComment, 'questionCommentRemove'>;
45
+ export {};
@@ -1,42 +1,80 @@
1
1
  import { gql } from '@apollo/client';
2
+ import { EQuestionLike, EQuestionType, } from '../../../models';
3
+ import { MARKSHEET, } from '../../query/restricted';
4
+ export const getLikeData = (like, item) => {
5
+ let { likes = 0, dislikes = 0, isLikedByMe } = item;
6
+ if (isLikedByMe === EQuestionLike.LIKE) {
7
+ likes = likes - 1;
8
+ if (like === EQuestionLike.DISLIKE) {
9
+ dislikes = dislikes + 1;
10
+ }
11
+ }
12
+ else if (isLikedByMe === EQuestionLike.DISLIKE) {
13
+ dislikes = dislikes - 1;
14
+ if (like === EQuestionLike.LIKE) {
15
+ likes = likes + 1;
16
+ }
17
+ }
18
+ else {
19
+ if (like === EQuestionLike.LIKE) {
20
+ likes = likes + 1;
21
+ }
22
+ else if (like === EQuestionLike.DISLIKE) {
23
+ dislikes = dislikes + 1;
24
+ }
25
+ }
26
+ return { likes, dislikes, isLikedByMe: like };
27
+ };
28
+ export const getQuestionTypeName = (typeId) => {
29
+ switch (typeId) {
30
+ case EQuestionType.QUESTION_ANSWER:
31
+ return 'QuestionQA';
32
+ case EQuestionType.PRESCRIPTION_ANSWER:
33
+ return 'QuestionPrescription';
34
+ case EQuestionType.MULTIPLE_ANSWERS:
35
+ return 'QuestionMultiA';
36
+ case EQuestionType.SINGLE_BEST_ANSWER:
37
+ default:
38
+ return 'QuestionSBA';
39
+ }
40
+ };
2
41
  export const QUESTION_LIKE = gql `
3
42
  mutation QuestionLike($questionId: Int!, $like: Int!) {
4
43
  restricted {
5
44
  questionLike(questionId: $questionId, like: $like) {
6
45
  id
7
46
  likes
47
+ typeId
8
48
  dislikes
9
49
  isLikedByMe
10
- comments {
11
- id
12
- createdAt
13
- comment
14
- likes
15
- user {
16
- id
17
- displayName
18
- }
19
- dislikes
20
- isLikedByMe
21
- questionId
22
- replies {
23
- id
24
- createdAt
25
- comment
26
- user {
27
- id
28
- displayName
29
- }
30
- likes
31
- dislikes
32
- isLikedByMe
33
- questionId
34
- }
35
- }
36
50
  }
37
51
  }
38
52
  }
39
53
  `;
54
+ export const optimisticQuestionLike = (marksheetId, client, input) => {
55
+ const { questionId, like } = input;
56
+ const data = client.readQuery({
57
+ variables: { id: marksheetId },
58
+ query: MARKSHEET,
59
+ });
60
+ const { marksheet } = data?.restricted || {};
61
+ const { marks = [] } = marksheet || {};
62
+ const { question } = marks.find(({ question }) => Number(question.id) === Number(questionId)) ||
63
+ {};
64
+ if (question) {
65
+ const { typeId } = question;
66
+ return {
67
+ restricted: {
68
+ questionLike: {
69
+ ...question,
70
+ __typename: getQuestionTypeName(typeId),
71
+ ...getLikeData(like, question),
72
+ },
73
+ },
74
+ };
75
+ }
76
+ return {};
77
+ };
40
78
  export const QUESTION_COMMENTS = gql `
41
79
  mutation QuestionComments(
42
80
  $questionId: Int!
@@ -52,11 +90,12 @@ export const QUESTION_COMMENTS = gql `
52
90
  id
53
91
  createdAt
54
92
  comment
55
- likes
93
+ parentId
56
94
  user {
57
95
  id
58
96
  displayName
59
97
  }
98
+ likes
60
99
  dislikes
61
100
  isLikedByMe
62
101
  questionId
@@ -64,6 +103,7 @@ export const QUESTION_COMMENTS = gql `
64
103
  id
65
104
  createdAt
66
105
  comment
106
+ parentId
67
107
  user {
68
108
  id
69
109
  displayName
@@ -77,6 +117,88 @@ export const QUESTION_COMMENTS = gql `
77
117
  }
78
118
  }
79
119
  `;
120
+ export const updateQuestionComments = (marksheetId) => (cache, result, options) => {
121
+ const { questionComments } = result?.data?.restricted || {};
122
+ const { variables } = options || {};
123
+ if (!variables || !questionComments) {
124
+ return;
125
+ }
126
+ const { questionId, parentId } = variables;
127
+ try {
128
+ const prevData = cache.readQuery({
129
+ variables: { id: marksheetId },
130
+ query: MARKSHEET,
131
+ });
132
+ if (prevData) {
133
+ const { marksheet } = prevData.restricted || {};
134
+ const { marks, ...marksheetRest } = marksheet;
135
+ const index = marks.findIndex(({ question }) => Number(question.id) === Number(questionId));
136
+ const { question, ...markRest } = marks[index];
137
+ const { comments = [], ...questionRest } = question;
138
+ let newComments;
139
+ if (parentId) {
140
+ const commentIndex = comments.findIndex(({ id }) => Number(id) === parentId);
141
+ const { replies = [], ...commentRest } = comments[commentIndex];
142
+ newComments = [
143
+ ...comments.slice(0, commentIndex),
144
+ { ...commentRest, replies: [...replies, questionComments] },
145
+ ...comments.slice(commentIndex + 1),
146
+ ];
147
+ }
148
+ else {
149
+ newComments = [...comments, questionComments];
150
+ }
151
+ cache.writeQuery({
152
+ query: MARKSHEET,
153
+ variables: { id: marksheetId },
154
+ data: {
155
+ ...prevData,
156
+ restricted: {
157
+ ...prevData.restricted,
158
+ marksheet: {
159
+ ...marksheetRest,
160
+ marks: [
161
+ ...marks.slice(0, index),
162
+ {
163
+ ...markRest,
164
+ question: { ...questionRest, comments: newComments },
165
+ },
166
+ ...marks.slice(index + 1),
167
+ ],
168
+ },
169
+ },
170
+ },
171
+ });
172
+ }
173
+ }
174
+ catch (error) {
175
+ console.error(error);
176
+ }
177
+ };
178
+ export const optimisticQuestionComment = (id, user, input) => {
179
+ const { parentId, questionId, comment } = input;
180
+ const { displayName, id: userId } = user || {};
181
+ const newComment = {
182
+ createdAt: new Date(),
183
+ id,
184
+ likes: 0,
185
+ questionId,
186
+ dislikes: 0,
187
+ isLikedByMe: EQuestionLike.NONE,
188
+ user: { displayName, id: userId },
189
+ comment,
190
+ parentId: parentId ? parentId : 0,
191
+ replies: [],
192
+ };
193
+ return {
194
+ restricted: {
195
+ questionComments: {
196
+ __typename: 'QuestionComments',
197
+ ...newComment,
198
+ },
199
+ },
200
+ };
201
+ };
80
202
  export const QUESTION_COMMENT_LIKE = gql `
81
203
  mutation QuestionCommentLike($commentId: Int!, $like: Int!) {
82
204
  restricted {
@@ -84,11 +206,12 @@ export const QUESTION_COMMENT_LIKE = gql `
84
206
  id
85
207
  createdAt
86
208
  comment
87
- likes
209
+ parentId
88
210
  user {
89
211
  id
90
212
  displayName
91
213
  }
214
+ likes
92
215
  dislikes
93
216
  isLikedByMe
94
217
  questionId
@@ -96,6 +219,7 @@ export const QUESTION_COMMENT_LIKE = gql `
96
219
  id
97
220
  createdAt
98
221
  comment
222
+ parentId
99
223
  user {
100
224
  id
101
225
  displayName
@@ -109,6 +233,21 @@ export const QUESTION_COMMENT_LIKE = gql `
109
233
  }
110
234
  }
111
235
  `;
236
+ export const optimisticCommentLike = (comments, input) => {
237
+ const { commentId, like } = input;
238
+ const comment = comments.find(({ id }) => Number(id) === Number(commentId)) ||
239
+ {};
240
+ return {
241
+ restricted: {
242
+ questionCommentLike: {
243
+ ...comment,
244
+ parentId: comment.parentId ? comment.parentId : 0,
245
+ __typename: 'QuestionComment',
246
+ ...getLikeData(like, comment),
247
+ },
248
+ },
249
+ };
250
+ };
112
251
  export const QUESTION_COMMENT_REMOVE = gql `
113
252
  mutation QuestionCommentRemove($commentId: Int!) {
114
253
  restricted {
@@ -116,11 +255,12 @@ export const QUESTION_COMMENT_REMOVE = gql `
116
255
  id
117
256
  createdAt
118
257
  comment
119
- likes
258
+ parentId
120
259
  user {
121
260
  id
122
261
  displayName
123
262
  }
263
+ likes
124
264
  dislikes
125
265
  isLikedByMe
126
266
  questionId
@@ -128,6 +268,7 @@ export const QUESTION_COMMENT_REMOVE = gql `
128
268
  id
129
269
  createdAt
130
270
  comment
271
+ parentId
131
272
  user {
132
273
  id
133
274
  displayName
@@ -141,3 +282,90 @@ export const QUESTION_COMMENT_REMOVE = gql `
141
282
  }
142
283
  }
143
284
  `;
285
+ export const updateQuestionCommentsRemove = (marksheetId) => (cache, result, options) => {
286
+ const { questionCommentRemove } = result?.data?.restricted || {};
287
+ const { variables } = options || {};
288
+ if (!variables || !questionCommentRemove) {
289
+ return;
290
+ }
291
+ const { questionId, id: commentId, parentId } = questionCommentRemove;
292
+ try {
293
+ const prevData = cache.readQuery({
294
+ variables: { id: marksheetId },
295
+ query: MARKSHEET,
296
+ });
297
+ if (prevData) {
298
+ const { marksheet } = prevData.restricted || {};
299
+ const { marks, ...marksheetRest } = marksheet;
300
+ const index = marks.findIndex(({ question }) => Number(question.id) === Number(questionId));
301
+ const { question, ...markRest } = marks[index];
302
+ const { comments = [], ...questionRest } = question;
303
+ let newComments;
304
+ if (parentId) {
305
+ const commentIndex = comments.findIndex(({ id }) => Number(id) === Number(parentId));
306
+ const { replies = [], ...commentRest } = comments[commentIndex];
307
+ newComments = [
308
+ ...comments.slice(0, commentIndex),
309
+ {
310
+ ...commentRest,
311
+ replies: replies.filter(({ id }) => Number(id) !== Number(commentId)),
312
+ },
313
+ ...comments.slice(commentIndex + 1),
314
+ ];
315
+ }
316
+ else {
317
+ newComments = comments.filter(({ id }) => Number(id) !== Number(commentId));
318
+ }
319
+ cache.writeQuery({
320
+ query: MARKSHEET,
321
+ variables: { id: marksheetId },
322
+ data: {
323
+ ...prevData,
324
+ restricted: {
325
+ ...prevData.restricted,
326
+ marksheet: {
327
+ ...marksheetRest,
328
+ marks: [
329
+ ...marks.slice(0, index),
330
+ {
331
+ ...markRest,
332
+ question: { ...questionRest, comments: newComments },
333
+ },
334
+ ...marks.slice(index + 1),
335
+ ],
336
+ },
337
+ },
338
+ },
339
+ });
340
+ }
341
+ }
342
+ catch (error) {
343
+ console.error(error);
344
+ }
345
+ };
346
+ export const optimisticQuestionCommentRemove = (comments, input, parentId) => {
347
+ const { commentId } = input;
348
+ let comment;
349
+ if (parentId) {
350
+ const parent = comments.find(({ id }) => Number(id) === Number(parentId)) ||
351
+ {};
352
+ comment =
353
+ parent?.replies?.find(({ id }) => Number(id) === Number(commentId)) ||
354
+ {};
355
+ }
356
+ else {
357
+ comment =
358
+ comments.find(({ id }) => Number(id) === Number(commentId)) ||
359
+ {};
360
+ }
361
+ return {
362
+ restricted: {
363
+ questionCommentRemove: {
364
+ __typename: 'QuestionComments',
365
+ ...comment,
366
+ parentId: parentId ? parentId : 0,
367
+ replies: comment.replies ? comment.replies : [],
368
+ },
369
+ },
370
+ };
371
+ };
@@ -0,0 +1,3 @@
1
+ import { ApolloClient } from '@apollo/client';
2
+ import { IMarksheetInput } from './restricted';
3
+ export declare const saveSampleMarkseehts: (client: ApolloClient<any>, marksheetInput: IMarksheetInput, questionIndex: number) => void;
@@ -0,0 +1,32 @@
1
+ import { SAMPLE_MARKSHEET, } from '../query';
2
+ export const saveSampleMarkseehts = (client, marksheetInput, questionIndex) => {
3
+ const { timeTaken, choiceId, mark, marksheetId } = marksheetInput;
4
+ const prevData = client.readQuery({
5
+ variables: { marksheetId },
6
+ query: SAMPLE_MARKSHEET,
7
+ });
8
+ if (prevData) {
9
+ const { sampleMarksheet } = prevData;
10
+ const { marks = [], ...rest } = sampleMarksheet || {};
11
+ client.writeQuery({
12
+ query: SAMPLE_MARKSHEET,
13
+ variables: { marksheetId },
14
+ data: {
15
+ ...prevData,
16
+ sampleMarksheet: {
17
+ ...rest,
18
+ marks: [
19
+ ...marks.slice(0, questionIndex),
20
+ {
21
+ ...marks[questionIndex],
22
+ timeTaken,
23
+ questionChoiceId: choiceId || null,
24
+ mark: mark || null,
25
+ },
26
+ ...marks.slice(questionIndex + 1),
27
+ ],
28
+ },
29
+ },
30
+ });
31
+ }
32
+ };
@@ -5,14 +5,18 @@ export const MARKSHEET = gql `
5
5
  marksheet(id: $id) {
6
6
  id
7
7
  createdAt
8
+ startedAt
8
9
  endedAt
9
10
  isTestMarksheet
10
11
  source
12
+ correct
13
+ incorrect
11
14
  marks {
12
15
  id
13
16
  flagged
14
17
  index
15
18
  questionChoiceId
19
+ marksheetId
16
20
  timeTaken
17
21
  mark
18
22
  question {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quesmed/types",
3
- "version": "2.1.7",
3
+ "version": "2.1.10",
4
4
  "description": "Typescript types for Quesmed",
5
5
  "keywords": [
6
6
  "quesmed",