Added field mapping support for members import CSVs

no issue

- This is a precursor work allowing client to send csv to JSON
field mapping hash as a `mapping` field parameter in the incoming
request.
This commit is contained in:
Nazar Gargol 2020-06-29 19:45:51 +12:00
parent f6f22fce7b
commit 6902f86b40
5 changed files with 61 additions and 10 deletions

View File

@ -47,6 +47,6 @@ module.exports = {
async importCSV(apiConfig, frame) {
debug('importCSV');
frame.data.members = await parse(frame.file.path);
frame.data.members = await parse(frame.file.path, frame.data.mapping);
}
};

View File

@ -53,7 +53,7 @@
"@tryghost/kg-mobiledoc-html-renderer": "3.0.1",
"@tryghost/magic-link": "0.4.9",
"@tryghost/members-api": "0.23.0",
"@tryghost/members-csv": "0.1.2",
"@tryghost/members-csv": "0.2.0",
"@tryghost/members-ssr": "0.8.1",
"@tryghost/mw-session-from-token": "0.1.4",
"@tryghost/session-service": "0.1.4",

View File

@ -136,7 +136,7 @@ describe('Members API', function () {
});
});
it('Can import CSV with minimum one field', function () {
it('Can import CSV with minimum one field and labels', function () {
return request
.post(localUtils.API.getApiQuery(`members/upload/`))
.field('labels', ['global-label-1', 'global-label-1'])
@ -184,6 +184,55 @@ describe('Members API', function () {
});
});
it('Can import CSV with mapped fields', function () {
return request
.post(localUtils.API.getApiQuery(`members/upload/`))
.field('mapping[email]', 'correo_electrpnico')
.field('mapping[name]', 'nombre')
.attach('membersfile', path.join(__dirname, '/../../../../utils/fixtures/csv/members-with-mappings.csv'))
.set('Origin', config.get('url'))
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(201)
.then((res) => {
should.not.exist(res.headers['x-cache-invalidate']);
const jsonResponse = res.body;
should.exist(jsonResponse);
should.exist(jsonResponse.meta);
should.exist(jsonResponse.meta.stats);
jsonResponse.meta.stats.imported.count.should.equal(1);
jsonResponse.meta.stats.invalid.count.should.equal(0);
})
.then(() => {
return request
.get(localUtils.API.getApiQuery(`members/?search=${encodeURIComponent('member+mapped_1@example.com')}`))
.set('Origin', config.get('url'))
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200);
})
.then((res) => {
should.not.exist(res.headers['x-cache-invalidate']);
const jsonResponse = res.body;
should.exist(jsonResponse);
should.exist(jsonResponse.members);
should.exist(jsonResponse.members[0]);
const importedMember1 = jsonResponse.members[0];
should(importedMember1.email).equal('member+mapped_1@example.com');
should(importedMember1.name).equal('Hannah');
should(importedMember1.note).equal('no need to map me');
importedMember1.subscribed.should.equal(true);
importedMember1.comped.should.equal(false);
importedMember1.stripe.should.not.be.undefined();
importedMember1.stripe.subscriptions.length.should.equal(0);
importedMember1.labels.length.should.equal(0);
});
});
it('Can import CSV with labels and provide additional labels', function () {
return request
.post(localUtils.API.getApiQuery(`members/upload/`))
@ -274,7 +323,7 @@ describe('Members API', function () {
should.exist(jsonResponse.new_today);
// 3 from fixtures and 5 imported in previous tests
jsonResponse.total.should.equal(7);
jsonResponse.total.should.equal(8);
});
});
@ -298,7 +347,7 @@ describe('Members API', function () {
should.exist(jsonResponse.new_today);
// 3 from fixtures and 5 imported in previous tests
jsonResponse.total.should.equal(7);
jsonResponse.total.should.equal(8);
});
});
@ -322,7 +371,7 @@ describe('Members API', function () {
should.exist(jsonResponse.new_today);
// 3 from fixtures and 5 imported in previous tests
jsonResponse.total.should.equal(7);
jsonResponse.total.should.equal(8);
});
});

View File

@ -0,0 +1,2 @@
correo_electrpnico,nombre,note
member+mapped_1@example.com,Hannah,"no need to map me"
1 correo_electrpnico nombre note
2 member+mapped_1@example.com Hannah no need to map me

View File

@ -503,10 +503,10 @@
node-jose "^1.1.3"
stripe "^7.4.0"
"@tryghost/members-csv@0.1.2":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@tryghost/members-csv/-/members-csv-0.1.2.tgz#c543125ed4cf3e02a2fcb3d98d365dd2e1796047"
integrity sha512-KJgtgbDQbgRS4K3OmulqZ64jIvm4+66f/RNhK0vwkf3HGgW5JEv5PVJJbVCu5IbwubwCYUjz/0K3oyl+BqIh0w==
"@tryghost/members-csv@0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@tryghost/members-csv/-/members-csv-0.2.0.tgz#33b65b9a5be79df38df9b0f18eb35f009df570e7"
integrity sha512-48B2DwKLq+xjpsinHYcgBO5ZHfJ6tQ3jwugu0ZhvAoSqB+v/QMrK82tYINvkaSGx03pLg4ft/PIpjhWzTipATg==
dependencies:
csv-parser "2.3.3"
papaparse "5.2.0"