-
Notifications
You must be signed in to change notification settings - Fork 78
/
config.js
752 lines (606 loc) · 19.3 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
import Mirage from 'ember-cli-mirage';
import Ember from 'ember';
import { paginate } from 'code-corps-ember/mirage/utils/pagination';
const {
isEmpty
} = Ember;
function generateCommentMentions(schema, comment) {
let body = comment.body || '';
let matches = body.match(/@\w+/g) || [];
matches.forEach((match) => {
let username = match.substr(1);
let [matchedUser] = schema.users.where({ username }).models;
if (matchedUser) {
let startIndex = body.indexOf(match);
let endIndex = startIndex + match.length - 1;
schema.create('commentUserMention', {
username,
indices: [startIndex, endIndex],
userId: matchedUser.id,
commentId: comment.id,
taskId: comment.taskId
});
}
});
}
function generateTaskMentions(schema, task) {
let body = task.body || '';
let matches = body.match(/@\w+/g) || [];
matches.forEach((match) => {
let username = match.substr(1);
let [matchedUser] = schema.users.where({ username }).models;
if (matchedUser) {
let startIndex = body.indexOf(match);
let endIndex = startIndex + match.length - 1;
schema.taskUserMentions.create({
username,
indices: [startIndex, endIndex],
userId: matchedUser.id,
taskId: task.id
});
}
});
}
function generatePreviewMentions(schema, preview) {
let body = preview.body || '';
let matches = body.match(/@\w+/g) || [];
matches.forEach((match) => {
let username = match.substr(1);
let [matchedUser] = schema.users.where({ username });
if (matchedUser) {
let startIndex = body.indexOf(match);
let endIndex = startIndex + match.length - 1;
schema.previewUserMentions.create({
username,
indices: [startIndex, endIndex],
userId: matchedUser.id,
previewId: preview.id
});
}
});
}
// The set of routes we have defined; needs updated when adding new routes
const routes = [
'categories', 'comment-user-mentions', 'comments', 'donation-goals',
'github-events', 'organizations', 'organization-invites', 'task-lists',
'task-skills', 'task-user-mentions', 'tasks', 'previews', 'projects',
'project-categories', 'slugged-routes', 'stripe-connect-accounts',
'stripe-connect-subscriptions', 'stripe-connect-plans',
'stripe-platform-cards', 'stripe-platform-customers', 'user-categories',
'users'
];
export default function() {
this.passthrough('https://api.stripe.com/**');
this.passthrough('https://uploads.stripe.com/**');
this.post('https://api.cloudinary.com/**', () => {
return new Mirage.Response(201, {}, {
public_id: 'abc123'
});
});
/**
* Categories
*/
this.get('/categories', { coalesce: true });
this.post('/categories');
this.get('/categories/:id');
this.patch('/categories/:id');
/**
* Comment user mentions
*/
// GET /comment-user-mentions
this.get('/comment-user-mentions', (schema, request) => {
let commentId = request.queryParams.comment_id;
let comment = schema.comments.find(commentId);
generateCommentMentions(schema, comment);
return schema.commentUserMentions.where({ commentId });
});
/**
* Comments
*/
this.get('/comments', { coalesce: true });
this.post('/comments', function(schema) {
let attrs = this.normalizedRequestAttrs();
// the API takes takes markdown and renders body
attrs.body = `<p>${attrs.markdown}</p>`;
return schema.create('comment', attrs);
});
this.get('/comments/:id');
this.patch('/comments/:id', function(schema) {
let attrs = this.normalizedRequestAttrs();
let comment = schema.comments.find(attrs.id);
// the API takes takes markdown and renders body
attrs.body = `<p>${attrs.markdown}</p>`;
comment.commentUserMentions.models.forEach((mention) => mention.destroy());
return comment.update(attrs);
});
/**
* Donation goals
*/
this.get('/donation-goals', { coalesce: true });
this.get('/donation-goals/:id');
this.patch('/donation-goals/:id');
this.post('/donation-goals');
/**
* Github App Installations
*/
this.get('/github-app-installations', { coalesce: true });
this.post('/github-app-installations');
this.get('/github-app-installations/:id');
/**
* Github Events
*/
this.get('/github-events', function(schema, request) {
let items = schema.githubEvents.all();
return paginate(items, '/github-events', request.queryParams);
});
this.get('/github-events/:id');
this.patch('/github-events/:id', function(schema) {
let attrs = this.normalizedRequestAttrs();
let { retry } = attrs;
let githubEvent = schema.githubEvents.find(attrs.id);
if (retry) {
// Simulate API retrying the event
return githubEvent.update({ status: 'reprocessing' });
} else {
// Simulate API failing to retry the event
return new Mirage.Response(422, {}, {
errors: [
{
detail: 'Retry must be accepted',
source: {
pointer: '/data/attributes/retry'
},
status: '422',
title: 'retry must be accepted'
}
],
jsonapi: {
version: '1.0'
}
});
}
});
/**
* Github Issues
*/
this.get('/github-issues', { coalesce: true });
this.get('/github-issues/:id');
/**
* Github Pull Requests
*/
this.get('/github-pull-requests', { coalesce: true });
this.get('/github-pull-requests/:id');
/**
* Github Repos
*/
this.get('/github-repos', { coalesce: true });
this.get('/github-repos/:id');
this.patch('/github-repos/:id', function(schema) {
let attrs = this.normalizedRequestAttrs();
let { projectId } = attrs;
let githubRepo = schema.githubRepos.find(attrs.id);
if (projectId) {
// Simulate API syncing the repo
let project = schema.projects.find(projectId);
return githubRepo.update({ project, syncState: 'synced' });
} else {
// Simulate API disconnecting the repo and unsetting the state
return githubRepo.update({ project: null, syncState: 'unsynced' });
}
});
/**
* Organizations
*/
this.get('/organizations', { coalesce: true });
this.post('/organizations');
this.get('/organizations/:id');
this.patch('/organizations/:id');
/**
* Organization Github App Installations
*/
this.get('/organization-github-app-installations', { coalesce: true });
this.post('/organization-github-app-installations');
this.get('/organization-github-app-installations/:id');
this.delete('/organization-github-app-installations/:id');
/**
* Organization Github App Installations
*/
this.get('/organization-invites', function(schema, request) {
let { queryParams } = request;
let { code } = queryParams;
if (code) {
return schema.organizationInvites.where({ code });
} else {
return schema.organizationInvites.all();
}
});
/**
* Password
*/
this.post('/password/forgot', () => {
// just return something?
return new Mirage.Response(201, {}, {
email: 'test@test.com'
});
});
this.post('/password/reset', function(schema) {
let { password, 'password-confirmation': passwordConfirmation, token } = this.normalizedRequestAttrs();
let [matchedUser] = schema.users.where({ token }).models;
if (password === passwordConfirmation) {
return new Mirage.Response(201, {}, {
email: matchedUser.email,
token,
user_id: matchedUser.id
});
} else {
return new Mirage.Response(422, {}, {
errors: [
{
detail: 'Password confirmation passwords do not match',
source: {
pointer: '/data/attributes/password-confirmation'
},
status: '422',
title: 'passwords do not match'
}
],
jsonapi: {
version: '1.0'
}
});
}
});
/**
* Previews
*/
this.post('/previews', function(schema) {
let attrs = this.normalizedRequestAttrs();
// the API takes takes markdown and renders body
attrs.body = `<p>${attrs.markdown}</p>`;
return schema.create('preview', attrs);
});
/**
* Preview user mentions
*/
this.get('/preview-user-mentions', (schema, request) => {
let previewId = request.queryParams.preview_id;
let preview = schema.previews.find(previewId);
generatePreviewMentions(schema, preview);
return schema.previewUserMentions.where({ previewId });
});
/**
* Project categories
*/
this.get('/project-categories', { coalesce: true });
this.post('/project-categories');
this.get('/project-categories/:id');
this.delete('/project-categories/:id');
/**
* Project skills
*/
this.get('/project-skills', { coalesce: true });
this.post('/project-skills');
this.get('/project-skills/:id');
this.delete('/project-skills/:id');
/**
* Project users
*/
this.get('/project-users', { coalesce: true });
this.post('/project-users');
this.get('/project-users/:id');
this.patch('/project-users/:id');
this.delete('/project-users/:id');
/**
* Projects
*/
this.get('/projects', { coalesce: true });
this.post('/projects');
this.get('/projects/:id');
this.get('/projects/:projectId/tasks', (schema, request) => {
let { projectId } = request.params;
let taskStatus = request.queryParams.status;
let pageNumber = parseInt(request.queryParams['page[page]']);
let pageSize = request.queryParams['page[page-size]'] || 10;
let project = schema.projects.find(projectId);
let { tasks } = project;
if (taskStatus) {
tasks = tasks.filter((p) => p.status === taskStatus);
}
let tasksPage = tasks.filter((p, index) => {
let pageNumberNotSpecified = !pageNumber;
let indexIsOnSpecifiedPage = (index >= (pageNumber - 1) * pageSize) && (index < pageNumber * pageSize);
return pageNumberNotSpecified || indexIsOnSpecifiedPage;
});
// hacky, but the only way I could find to pass in a mocked meta object
// for our pagination tests
tasksPage.meta = {
'total_records': tasks.models.length,
'total_pages': Math.ceil(tasks.models.length / pageSize),
'page_size': pageSize,
'current_page': pageNumber || 1
};
return tasksPage;
});
this.get('/projects/:projectId/tasks/:number', (schema, request) => {
let projectId = parseInt(request.params.projectId);
let number = parseInt(request.params.number);
let project = schema.projects.find(projectId);
let [task] = project.tasks.filter((p) => {
return p.number === number;
}).models;
task.comments.models.forEach((comment) => {
generateCommentMentions(schema, comment);
});
return task;
});
this.patch('/projects/:id', function(schema) {
// the API takes takes markdown and renders body
let attrs = this.normalizedRequestAttrs();
let project = schema.projects.find(attrs.id);
attrs.longDescriptionBody = `<p>${attrs.longDescriptionMarkdown}</p>`;
return project.update(attrs);
});
/**
* Roles
*/
this.get('/roles', { coalesce: true });
this.post('/roles');
this.get('/roles/:id');
/**
* Role Skills
*/
this.get('/role-skills', { coalesce: true });
this.post('/role-skills');
this.get('/role-skills/:id');
this.delete('/role-skills/:id');
/**
* Slugs and slugged routes
*/
// GET /:slug
this.get('/:slug', (schema, request) => {
if (routes.includes(request.params.slug)) {
console.error('API route being caught in /:slug in mirage/config.js', request.params.slug);
}
return schema.sluggedRoutes.where({ 'slug': request.params.slug }).models[0];
});
// GET /:slug/projects
this.get('/:slug/projects', (schema, request) => {
let { slug } = request.params;
let [organization] = schema.organizations.where({ slug }).models;
return organization.projects;
});
// GET /:slug/:project_slug
this.get('/:sluggedRouteSlug/:projectSlug', (schema, request) => {
let { sluggedRouteSlug, projectSlug } = request.params;
let [sluggedRoute] = schema.sluggedRoutes.where({ 'slug': sluggedRouteSlug }).models;
if (!sluggedRoute) {
// If there's no slugged route, we likely added a new route and didn't
// `routes` array above.
console.error(`Did you add a new route under /${sluggedRouteSlug} (/${projectSlug}) without adding it to mirage/config.js?`);
}
return sluggedRoute.organization.projects.filter((p) => {
return p.slug === projectSlug;
}).models[0];
});
/**
* Skills
*/
this.get('/skills', { coalesce: true });
this.post('/skills');
this.get('/skills/:id');
/**
* Stripe connect accounts
*/
this.post('/stripe-connect-accounts', function(schema) {
let attrs = this.normalizedRequestAttrs();
attrs.recipientStatus = 'required';
return schema.create('stripeConnectAccount', attrs);
});
this.get('/stripe-connect-accounts/:id');
this.patch('/stripe-connect-accounts/:id', function(schema) {
let attrs = this.normalizedRequestAttrs();
let stripeConnectAccount = schema.stripeConnectAccounts.find(attrs.id);
if (!isEmpty(attrs.legalEntityAddressCity)) {
attrs.recipientStatus = 'verifying';
attrs.personalIdNumberStatus = 'required';
}
if (!isEmpty(attrs.legalEntityPersonalIdNumber)) {
attrs.personalIdNumberStatus = 'verified';
attrs.verificationDocumentStatus = 'required';
}
if (!isEmpty(attrs.legalEntityVerificationDocument)) {
attrs.recipientStatus = 'verified';
attrs.verificationDocumentStatus = 'verified';
attrs.bankAccountStatus = 'required';
}
if (!isEmpty(attrs.externalAccount)) {
attrs.bankAccountStatus = 'verified';
}
return stripeConnectAccount.update(attrs);
});
/**
* Stripe plans
*/
this.post('/stripe-connect-plans');
this.get('/stripe-connect-plans/:id');
/**
* Stripe platform cards
*/
this.post('/stripe-platform-cards');
this.get('/stripe-platform-cards/:id');
/**
* Stripe customers
*/
this.post('/stripe-platform-customers');
this.get('/stripe-platform-customers/:id');
/**
* Stripe subscriptions
*/
this.post('/stripe-connect-subscriptions');
this.get('/stripe-connect-subscriptions/:id');
/**
* Task lists
*/
this.get('/task-lists', { coalesce: true });
this.get('/task-lists/:id');
/**
* Task user mentions
*/
// GET /task-user-mentions
this.get('/task-user-mentions', (schema, request) => {
let taskId = request.queryParams.task_id;
let task = schema.tasks.find(taskId);
generateTaskMentions(schema, task);
return schema.taskUserMentions.where({ taskId });
});
/**
* Tasks
*/
// GET /tasks
this.get('/tasks', { coalesce: true });
// POST /tasks
this.post('/tasks', function(schema) {
let attrs = this.normalizedRequestAttrs();
let project = schema.projects.find(attrs.projectId);
// the API takes takes markdown and renders body
attrs.body = `<p>${attrs.markdown}</p>`;
// the API sets task number as an auto-incrementing value, scoped
// to project, so we need to simulate that here
attrs.number = project.tasks.models.length + 1;
return schema.create('task', attrs);
});
// GET /tasks/:id
this.get('/tasks/:id');
// PATCH /tasks/:id
this.patch('/tasks/:id', function(schema) {
let attrs = this.normalizedRequestAttrs();
let task = schema.tasks.find(attrs.id);
// the API takes takes markdown and renders body
attrs.body = `<p>${attrs.markdown}</p>`;
task.taskUserMentions.models.forEach((mention) => mention.destroy());
task.order = (task.position || 0) * 100;
return task.update(attrs);
});
/**
* Task skills
*/
this.get('/task-skills', { coalesce: true });
this.post('/task-skills');
this.get('/task-skills/:id');
this.delete('/task-skills/:id');
/**
* Token
*/
// POST /token
this.post('/token', (schema, request) => {
let json = JSON.parse(request.requestBody);
let { models } = schema.users.where({ email: json.username, password: json.password });
if (models.length > 0) {
return {
// token encoded at https://jwt.io/
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXNzd29yZCI6InBhc3N3b3JkIiwidXNlcm5hbWUiOiJvd25lckBjb2RlY29ycHMub3JnIiwidXNlcl9pZCI6MSwiZXhwIjo3MjAwMDAwMH0.LxkkKMcQoccAA0pphgRfXPSLdyaCawlK1gB3yPCht2s',
user_id: models[0].id
};
} else {
let errorDetail = `Your password doesn't match the email ${json.username}.`;
return new Mirage.Response(401, {}, {
errors: [
{
id: 'UNAUTHORIZED',
title: '401 Unauthorized',
detail: errorDetail,
status: 401
}
]
});
}
});
/**
* Users
*/
this.get('/users', { coalesce: true });
this.post('/users', function(schema) {
let { email, password, username, state = 'signed_up', signUpContext } = this.normalizedRequestAttrs();
let user = schema.create('user', { email, password, state, username, signUpContext });
schema.create('sluggedRoute', { slug: user.username, user });
return user;
});
this.get('/users/:id');
// PATCH /users/:id
this.patch('/users/:id', function(schema) {
let attrs = this.normalizedRequestAttrs();
let userId = attrs.id;
let user = schema.users.find(userId);
// Mock out state machine
if (!isEmpty(attrs.stateTransition)) {
switch (attrs.stateTransition) {
case 'edit_profile':
attrs.state = 'edited_profile';
break;
case 'select_categories':
attrs.state = 'selected_categories';
break;
case 'select_roles':
attrs.state = 'selected_roles';
break;
case 'select_skills':
attrs.state = 'selected_skills';
break;
default:
console.error('You added a transition without changing the state machine in Mirage.');
break;
}
}
return user.update(attrs);
});
// GET /users/email_available
this.get('/users/email_available', (schema, request) => {
let { email } = request.queryParams;
let { models } = schema.users.where({ email });
let available = models.length === 0;
return { available, valid: true };
});
// GET /users/username_available
this.get('/users/username_available', (schema, request) => {
let { username } = request.queryParams;
let { models } = schema.users.where({ username });
let available = models.length === 0;
return { available, valid: true };
});
/**
* User categories
*/
// GET /user-categories
this.get('/user-categories', { coalesce: true });
// POST /user-categories
this.post('/user-categories');
// GET /user-categories/:id
this.get('/user-categories/:id');
// DELETE /user-categories/:id
this.delete('/user-categories/:id');
/**
* User roles
*/
this.get('/user-roles', { coalesce: true });
this.post('/user-roles');
this.get('/user-roles/:id');
this.delete('/user-roles/:id');
/**
* User skills
*/
this.get('/user-skills', { coalesce: true });
this.post('/user-skills');
this.get('/user-skills/:id');
this.delete('/user-skills/:id');
/**
* User tasks
*/
this.get('/user-tasks', { coalesce: true });
this.patch('/user-tasks/:id');
this.post('/user-tasks');
this.get('/user-tasks/:id');
this.delete('/user-tasks/:id');
// Create a passthrough for ember-cli-code-coverage
// https://github.com/kategengler/ember-cli-code-coverage
this.passthrough('/write-coverage');
}