2022-04-13 21:34:48 +03:00
|
|
|
import {camelize} from '@ember/string';
|
2022-06-02 14:19:52 +03:00
|
|
|
import {dasherize} from '@ember/string';
|
|
|
|
import {isBlank} from '@ember/utils';
|
2022-04-04 21:26:49 +03:00
|
|
|
import {paginatedResponse} from '../utils';
|
|
|
|
|
|
|
|
export default function mockNewsletters(server) {
|
|
|
|
server.get('/newsletters/', paginatedResponse('newsletters'));
|
|
|
|
server.get('/newsletters/:id/');
|
2022-04-13 21:34:48 +03:00
|
|
|
|
2022-04-19 22:01:08 +03:00
|
|
|
server.post('/newsletters/', function ({newsletters}, {queryParams}) {
|
2022-04-13 21:34:48 +03:00
|
|
|
const attrs = this.normalizedRequestAttrs();
|
|
|
|
|
|
|
|
// sender email can't be set without verification
|
|
|
|
const senderEmail = attrs.senderEmail;
|
|
|
|
attrs.senderEmail = null;
|
|
|
|
|
2022-06-02 14:19:52 +03:00
|
|
|
if (isBlank(attrs.slug) && !isBlank(attrs.name)) {
|
|
|
|
attrs.slug = dasherize(attrs.name);
|
|
|
|
}
|
|
|
|
|
2022-04-13 21:34:48 +03:00
|
|
|
const newsletter = newsletters.create(attrs);
|
|
|
|
|
|
|
|
// workaround for mirage output of meta
|
|
|
|
const collection = newsletters.where({id: newsletter.id});
|
|
|
|
|
|
|
|
if (senderEmail) {
|
|
|
|
collection.meta = {
|
|
|
|
sent_email_verification: ['sender_email']
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-19 22:01:08 +03:00
|
|
|
if (queryParams.opt_in_existing === 'true') {
|
|
|
|
newsletters.all().models.forEach((n) => {
|
|
|
|
newsletter.members.mergeCollection(n.members);
|
|
|
|
});
|
|
|
|
newsletter.save();
|
|
|
|
}
|
|
|
|
|
2022-04-13 21:34:48 +03:00
|
|
|
return collection;
|
|
|
|
});
|
|
|
|
|
|
|
|
server.put('/newsletters/:id/', function ({newsletters}, {params}) {
|
|
|
|
const attrs = this.normalizedRequestAttrs();
|
|
|
|
const newsletter = newsletters.find(params.id);
|
|
|
|
|
|
|
|
const previousSenderEmail = newsletter.senderEmail;
|
|
|
|
const newSenderEmail = attrs.senderEmail;
|
|
|
|
|
|
|
|
// sender email can't be changed without verification
|
|
|
|
if (newSenderEmail && newSenderEmail !== previousSenderEmail) {
|
2022-05-18 14:12:43 +03:00
|
|
|
// It doesn't correctly return to the previous email if previousSenderEmail is undefined
|
|
|
|
attrs.senderEmail = previousSenderEmail === undefined ? null : previousSenderEmail;
|
2022-04-13 21:34:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
newsletter.update(attrs);
|
|
|
|
|
|
|
|
// workaround for mirage output of meta
|
|
|
|
const collection = newsletters.where({id: newsletter.id});
|
|
|
|
|
|
|
|
if (newSenderEmail && newSenderEmail !== previousSenderEmail) {
|
|
|
|
collection.meta = {
|
|
|
|
sent_email_verification: ['sender_email']
|
|
|
|
};
|
|
|
|
|
|
|
|
const tokenData = {
|
|
|
|
id: newsletter.id,
|
|
|
|
email: newSenderEmail,
|
|
|
|
type: 'sender_email'
|
|
|
|
};
|
|
|
|
const token = btoa(JSON.stringify(tokenData));
|
|
|
|
const baseUrl = window.location.href.replace(window.location.hash, '');
|
2022-04-25 18:54:48 +03:00
|
|
|
const verifyUrl = `${baseUrl}settings/newsletters/?verifyEmail=${token}`;
|
2022-04-13 21:34:48 +03:00
|
|
|
// eslint-disable-next-line
|
|
|
|
console.warn('Verification email sent. Mocked verification URL:', verifyUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return collection;
|
|
|
|
});
|
|
|
|
|
|
|
|
// verify email update
|
2022-04-19 20:06:56 +03:00
|
|
|
server.put('/newsletters/verifications/', function ({newsletters}, request) {
|
2022-04-13 21:34:48 +03:00
|
|
|
const requestBody = JSON.parse(request.requestBody);
|
|
|
|
const tokenData = JSON.parse(atob(requestBody.token));
|
|
|
|
|
|
|
|
const newsletter = newsletters.find(tokenData.id);
|
|
|
|
|
|
|
|
newsletter[camelize(tokenData.type)] = tokenData.email;
|
|
|
|
|
|
|
|
return newsletter.save();
|
|
|
|
});
|
2022-04-04 21:26:49 +03:00
|
|
|
}
|