mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-26 04:13:30 +03:00
Name for custom newsletters (#18124)
refs https://github.com/TryGhost/Product/issues/3862 & https://github.com/TryGhost/Product/issues/3863
This commit is contained in:
parent
d5bcd42496
commit
f243083bfa
@ -17,7 +17,8 @@ const messages = {
|
|||||||
memberNotFoundSignUp: 'No member exists with this e-mail address. Please sign up first.',
|
memberNotFoundSignUp: 'No member exists with this e-mail address. Please sign up first.',
|
||||||
invalidType: 'Invalid checkout type.',
|
invalidType: 'Invalid checkout type.',
|
||||||
notConfigured: 'This site is not accepting payments at the moment.',
|
notConfigured: 'This site is not accepting payments at the moment.',
|
||||||
invalidNewsletterId: 'Cannot subscribe to invalid newsletter {ids}'
|
invalidNewsletters: 'Cannot subscribe to invalid newsletters {newsletters}',
|
||||||
|
archivedNewsletters: 'Cannot subscribe to archived newsletters {newsletters}'
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = class RouterController {
|
module.exports = class RouterController {
|
||||||
@ -483,19 +484,30 @@ module.exports = class RouterController {
|
|||||||
// Validate requested newsletters
|
// Validate requested newsletters
|
||||||
let {newsletters: requestedNewsletters} = req.body;
|
let {newsletters: requestedNewsletters} = req.body;
|
||||||
|
|
||||||
if (requestedNewsletters && requestedNewsletters.length > 0) {
|
if (requestedNewsletters && requestedNewsletters.length > 0 && requestedNewsletters.every(newsletter => newsletter.name !== undefined)) {
|
||||||
const newsletterIds = requestedNewsletters.map(newsletter => newsletter.id);
|
const newsletterNames = requestedNewsletters.map(newsletter => newsletter.name);
|
||||||
|
const newsletterNamesFilter = newsletterNames.map(newsletter => `'${newsletter.replace(/("|')/g, '\\$1')}'`);
|
||||||
const newsletters = await this._newslettersService.browse({
|
const newsletters = await this._newslettersService.browse({
|
||||||
filter: `id:[${newsletterIds}]`,
|
filter: `name:[${newsletterNamesFilter}]`,
|
||||||
columns: ['id','status']
|
columns: ['id','name','status']
|
||||||
});
|
});
|
||||||
|
|
||||||
if (newsletters.length !== newsletterIds.length) {
|
if (newsletters.length !== newsletterNames.length) { //check for invalid newsletters
|
||||||
const validNewsletterIds = newsletters.map(newsletter => newsletter.id);
|
const validNewsletters = newsletters.map(newsletter => newsletter.name);
|
||||||
const invalidNewsletterIds = newsletterIds.filter(id => !validNewsletterIds.includes(id));
|
const invalidNewsletters = newsletterNames.filter(newsletter => !validNewsletters.includes(newsletter));
|
||||||
|
|
||||||
throw new errors.BadRequestError({
|
throw new errors.BadRequestError({
|
||||||
message: tpl(messages.invalidNewsletterId, {ids: invalidNewsletterIds})
|
message: tpl(messages.invalidNewsletters, {newsletters: invalidNewsletters})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//validation for archived newsletters
|
||||||
|
const archivedNewsletters = newsletters
|
||||||
|
.filter(newsletter => newsletter.status === 'archived')
|
||||||
|
.map(newsletter => newsletter.name);
|
||||||
|
if (archivedNewsletters && archivedNewsletters.length > 0) {
|
||||||
|
throw new errors.BadRequestError({
|
||||||
|
message: tpl(messages.archivedNewsletters, {newsletters: archivedNewsletters})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,29 +126,33 @@ describe('RouterController', function () {
|
|||||||
const newsletters = [
|
const newsletters = [
|
||||||
{
|
{
|
||||||
id: 'abc123',
|
id: 'abc123',
|
||||||
|
name: 'Newsletter 1',
|
||||||
status: 'active'
|
status: 'active'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'def456',
|
id: 'def456',
|
||||||
|
name: 'Newsletter 2',
|
||||||
status: 'active'
|
status: 'active'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'ghi789',
|
id: 'ghi789',
|
||||||
|
name: 'Newsletter 3',
|
||||||
status: 'active'
|
status: 'active'
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
req.body.newsletters = newsletters.map(newsletter => ({id: newsletter.id}));
|
req.body.newsletters = newsletters.map(newsletter => ({name: newsletter.name}));
|
||||||
|
|
||||||
const newsletterIds = newsletters.map(newsletter => newsletter.id);
|
const newsletterNames = newsletters.map(newsletter => newsletter.name);
|
||||||
|
const newsletterNamesFilter = newsletterNames.map(newsletter => `'${newsletter.replace(/("|')/g, '\\$1')}'`);
|
||||||
const newslettersServiceStub = {
|
const newslettersServiceStub = {
|
||||||
browse: sinon.stub()
|
browse: sinon.stub()
|
||||||
};
|
};
|
||||||
|
|
||||||
newslettersServiceStub.browse
|
newslettersServiceStub.browse
|
||||||
.withArgs({
|
.withArgs({
|
||||||
filter: `id:[${newsletterIds}]`,
|
filter: `name:[${newsletterNamesFilter}]`,
|
||||||
columns: ['id', 'status']
|
columns: ['id','name','status']
|
||||||
})
|
})
|
||||||
.resolves(newsletters);
|
.resolves(newsletters);
|
||||||
|
|
||||||
@ -170,10 +174,10 @@ describe('RouterController', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('validates specified newsletters', async function () {
|
it('validates specified newsletters', async function () {
|
||||||
const INVALID_NEWSLETTER_ID = 'abc123';
|
const INVALID_NEWSLETTER_NAME = 'abc123';
|
||||||
|
|
||||||
req.body.newsletters = [
|
req.body.newsletters = [
|
||||||
{id: INVALID_NEWSLETTER_ID}
|
{name: INVALID_NEWSLETTER_NAME}
|
||||||
];
|
];
|
||||||
|
|
||||||
const newslettersServiceStub = {
|
const newslettersServiceStub = {
|
||||||
@ -182,8 +186,8 @@ describe('RouterController', function () {
|
|||||||
|
|
||||||
newslettersServiceStub.browse
|
newslettersServiceStub.browse
|
||||||
.withArgs({
|
.withArgs({
|
||||||
filter: `id:[${INVALID_NEWSLETTER_ID}]`,
|
filter: `name:['${INVALID_NEWSLETTER_NAME}']`,
|
||||||
columns: ['id', 'status']
|
columns: ['id','name','status']
|
||||||
})
|
})
|
||||||
.resolves([]);
|
.resolves([]);
|
||||||
|
|
||||||
@ -191,36 +195,39 @@ describe('RouterController', function () {
|
|||||||
newslettersService: newslettersServiceStub
|
newslettersService: newslettersServiceStub
|
||||||
});
|
});
|
||||||
|
|
||||||
await controller.sendMagicLink(req, res).should.be.rejectedWith(`Cannot subscribe to invalid newsletter ${INVALID_NEWSLETTER_ID}`);
|
await controller.sendMagicLink(req, res).should.be.rejectedWith(`Cannot subscribe to invalid newsletters ${INVALID_NEWSLETTER_NAME}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not add specified newsletters to the tokenData if they are archived', async function () {
|
it('validates archived newsletters', async function () {
|
||||||
const newsletters = [
|
const newsletters = [
|
||||||
{
|
{
|
||||||
id: 'abc123',
|
id: 'abc123',
|
||||||
|
name: 'Newsletter 1',
|
||||||
status: 'active'
|
status: 'active'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'def456',
|
id: 'def456',
|
||||||
|
name: 'Newsletter 2',
|
||||||
status: 'archived'
|
status: 'archived'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'ghi789',
|
id: 'ghi789',
|
||||||
|
name: 'Newsletter 3',
|
||||||
status: 'active'
|
status: 'active'
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
req.body.newsletters = newsletters.map(newsletter => ({id: newsletter.id}));
|
req.body.newsletters = newsletters.map(newsletter => ({name: newsletter.name}));
|
||||||
|
|
||||||
const newsletterIds = newsletters.map(newsletter => newsletter.id);
|
const newsletterNames = newsletters.map(newsletter => `'${newsletter.name}'`);
|
||||||
const newslettersServiceStub = {
|
const newslettersServiceStub = {
|
||||||
browse: sinon.stub()
|
browse: sinon.stub()
|
||||||
};
|
};
|
||||||
|
|
||||||
newslettersServiceStub.browse
|
newslettersServiceStub.browse
|
||||||
.withArgs({
|
.withArgs({
|
||||||
filter: `id:[${newsletterIds}]`,
|
filter: `name:[${newsletterNames}]`,
|
||||||
columns: ['id', 'status']
|
columns: ['id', 'name','status']
|
||||||
})
|
})
|
||||||
.resolves(newsletters);
|
.resolves(newsletters);
|
||||||
|
|
||||||
@ -228,16 +235,7 @@ describe('RouterController', function () {
|
|||||||
newslettersService: newslettersServiceStub
|
newslettersService: newslettersServiceStub
|
||||||
});
|
});
|
||||||
|
|
||||||
await controller.sendMagicLink(req, res);
|
await controller.sendMagicLink(req, res).should.be.rejectedWith(`Cannot subscribe to archived newsletters Newsletter 2`);
|
||||||
|
|
||||||
res.writeHead.calledOnceWith(201).should.be.true();
|
|
||||||
res.end.calledOnceWith('Created.').should.be.true();
|
|
||||||
|
|
||||||
sendEmailWithMagicLinkStub.calledOnce.should.be.true();
|
|
||||||
sendEmailWithMagicLinkStub.args[0][0].tokenData.newsletters.should.eql([
|
|
||||||
{id: newsletters[0].id},
|
|
||||||
{id: newsletters[2].id}
|
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user