2022-09-23 20:15:08 +03:00
|
|
|
import moment from 'moment-timezone';
|
2022-03-08 14:32:01 +03:00
|
|
|
import {Response} from 'miragejs';
|
2017-01-02 21:49:44 +03:00
|
|
|
import {paginatedResponse} from '../utils';
|
|
|
|
|
|
|
|
export default function mockInvites(server) {
|
|
|
|
server.get('/invites/', paginatedResponse('invites'));
|
|
|
|
|
|
|
|
server.get('/invites/:id', function (schema, request) {
|
|
|
|
let {id} = request.params;
|
|
|
|
let invite = schema.invites.find(id);
|
|
|
|
|
|
|
|
return invite || new Response(404, {}, {
|
|
|
|
errors: [{
|
2019-03-25 14:29:14 +03:00
|
|
|
type: 'NotFoundError',
|
2017-01-02 21:49:44 +03:00
|
|
|
message: 'Invite not found.'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
server.post('/invites/', function ({invites}) {
|
|
|
|
let attrs = this.normalizedRequestAttrs();
|
|
|
|
let oldInvite = invites.findBy({email: attrs.email});
|
|
|
|
|
|
|
|
if (oldInvite) {
|
|
|
|
oldInvite.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
attrs.token = `${invites.all().models.length}-token`;
|
2017-02-27 16:11:39 +03:00
|
|
|
attrs.expires = moment.utc().add(1, 'day').valueOf();
|
2017-09-28 05:33:00 +03:00
|
|
|
attrs.createdAt = moment.utc().format();
|
|
|
|
attrs.createdBy = 1;
|
|
|
|
attrs.updatedAt = moment.utc().format();
|
|
|
|
attrs.updatedBy = 1;
|
2017-01-02 21:49:44 +03:00
|
|
|
attrs.status = 'sent';
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
|
|
|
|
return invites.create(attrs);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.del('/invites/:id/');
|
|
|
|
}
|