2016-11-14 16:16:51 +03:00
|
|
|
/* eslint-disable camelcase */
|
2017-01-02 21:49:44 +03:00
|
|
|
import {Response} from 'ember-cli-mirage';
|
2016-08-17 18:14:33 +03:00
|
|
|
import {paginatedResponse} from '../utils';
|
|
|
|
|
|
|
|
export default function mockSubscribers(server) {
|
2017-01-02 21:49:44 +03:00
|
|
|
server.get('/subscribers/', paginatedResponse('subscribers'));
|
2016-08-17 18:14:33 +03:00
|
|
|
|
2017-09-28 05:33:00 +03:00
|
|
|
server.post('/subscribers/', function ({subscribers}) {
|
|
|
|
let attrs = this.normalizedRequestAttrs();
|
2017-01-02 21:49:44 +03:00
|
|
|
let subscriber = subscribers.findBy({email: attrs.email});
|
2016-08-17 18:14:33 +03:00
|
|
|
|
|
|
|
if (subscriber) {
|
2017-01-02 21:49:44 +03:00
|
|
|
return new Response(422, {}, {
|
2016-08-17 18:14:33 +03:00
|
|
|
errors: [{
|
2019-03-25 14:29:14 +03:00
|
|
|
type: 'ValidationError',
|
2019-05-07 14:39:56 +03:00
|
|
|
message: 'Validation error, cannot save subscriber.',
|
|
|
|
context: 'Email address is already subscribed.',
|
|
|
|
property: null
|
2016-08-17 18:14:33 +03:00
|
|
|
}]
|
|
|
|
});
|
|
|
|
} else {
|
2017-09-28 05:33:00 +03:00
|
|
|
attrs.createdAt = new Date();
|
|
|
|
attrs.createdBy = 0;
|
2016-08-17 18:14:33 +03:00
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
return subscribers.create(attrs);
|
2016-08-17 18:14:33 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
server.put('/subscribers/:id/');
|
2016-08-17 18:14:33 +03:00
|
|
|
|
2016-11-14 16:16:51 +03:00
|
|
|
server.post('/subscribers/csv/', function () {
|
2016-08-17 18:14:33 +03:00
|
|
|
// NB: we get a raw FormData object with no way to inspect it in Chrome
|
|
|
|
// until version 50 adds the additional read methods
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/FormData#Browser_compatibility
|
|
|
|
|
|
|
|
server.createList('subscriber', 50);
|
|
|
|
|
|
|
|
return {
|
|
|
|
meta: {
|
|
|
|
stats: {
|
|
|
|
imported: 50,
|
|
|
|
duplicates: 3,
|
|
|
|
invalid: 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2017-01-02 21:49:44 +03:00
|
|
|
|
|
|
|
server.del('/subscribers/:id/');
|
2016-08-17 18:14:33 +03:00
|
|
|
}
|