From fefbf15f5e1f310e321fc5c6a683a08c9dc19657 Mon Sep 17 00:00:00 2001 From: lefuturiste Date: Mon, 27 Jul 2020 12:06:09 +0000 Subject: [PATCH] fix: minor fixes and add csv import support --- assets/development/scripts/home.js | 4 +- package.json | 2 +- .../AdminOrganizationController.ts | 39 ++++++++++++++++--- src/controllers/PublicController.ts | 34 ++++++++-------- src/models/Organization.ts | 18 ++++++++- yarn.lock | 8 ++-- 6 files changed, 73 insertions(+), 32 deletions(-) diff --git a/assets/development/scripts/home.js b/assets/development/scripts/home.js index 40252ad..57e0798 100644 --- a/assets/development/scripts/home.js +++ b/assets/development/scripts/home.js @@ -94,8 +94,8 @@ function renderCard(organization) { // image let image = createEl('card-image-container') let imageTag = createEl('card-image') - //mediaBaseUrl + '/' + - imageTag.style = `background-image: url('${organization.thumbnail}')` + + imageTag.style = `background-image: url('${mediaBaseUrl + '/' + organization.thumbnail}')` image.appendChild(imageTag) card.appendChild(image) diff --git a/package.json b/package.json index b3a71fb..90bc029 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "@types/multer-s3": "^2.7.7", "@types/mustache": "^4.0.1", "@types/nodemailer": "^6.4.0", - "@types/papaparse": "^5.0.4", + "@types/papaparse": "^5.0.6", "@types/sanitize-html": "^1.23.3", "@types/twig": "^1.12.3", "aws-sdk": "^2.706.0", diff --git a/src/controllers/AdminOrganizationController.ts b/src/controllers/AdminOrganizationController.ts index 180c4f3..473e98c 100644 --- a/src/controllers/AdminOrganizationController.ts +++ b/src/controllers/AdminOrganizationController.ts @@ -6,6 +6,7 @@ import slugify from 'slugify' import { Document } from 'mongoose' import MediaService from '../MediaService' import Utils from '../Utils' +import Papa from 'papaparse' export default class AdminOrganizationController { static getMany(req: express.Request, res: express.Response) { @@ -21,10 +22,37 @@ export default class AdminOrganizationController { static import(req: express.Request, res: express.Response) { // first column is always name // second column is always email - const data = req.body.csv - res.json({ - success: true, - data + const input = req.body.data + const parsed = Papa.parse(input) + if (parsed.errors.length > 0) { + return res + .status(400) + .json({ + success: false, + errors: parsed.errors, + _note: 'Invalid import data or csv' + }) + } + let promises = parsed.data.map(d => { + return AdminOrganizationController.storeUniversal( + d[0], + d[1].replace(' ', ''), + 'unaware' + ) + }) + Promise.all(promises).then(data => { + res.json({ + success: true, + input, + parsed, + data + }) + }).catch(errors => { + console.log(errors) + res.status(400).json({ + success: false, + errors + }) }) } @@ -78,9 +106,8 @@ export default class AdminOrganizationController { } } } - console.log(body) Organization.create(body).then(data => { - AdminOrganizationController.sendEmailTokenUniversal(data) + //AdminOrganizationController.sendEmailTokenUniversal(data) resolve({ data, body }) }).catch(err => reject(err)) }) diff --git a/src/controllers/PublicController.ts b/src/controllers/PublicController.ts index a7b1c3a..e8c3fd3 100644 --- a/src/controllers/PublicController.ts +++ b/src/controllers/PublicController.ts @@ -31,23 +31,23 @@ export default class PublicController { if (!isProposed) { organizations = organizations.filter(o => o.get('publishedAt') !== undefined && o.get('publishedAt') !== null) } - // let organizationsData = organizations - // .map(o => { - // const version = isProposed ? o.get('proposedVersion'): o.get('publishedVersion') - // return { - // _id: o._id, - // name: version.name, - // description: version.descriptionShort, - // thumbnail: version.thumbnail.key, - // tags: version.tags === null ? 'tags_not_found' : version.tags, - // slug: o.get('slugs')[o.get('slugs).length -1] - // } - // }) - let lorem = "Dolore sit tempor et duo ipsum sit sed takimata, et magna voluptua ut sed justo eirmod. Sed tempor justo magna accusam aliquyam sea invidunt eos. Aliquyam accusam vero accusam sed" - let organizationsData = [] - for (var i = 0; i < 40; i++) { - organizationsData.push({ _id: i, name: 'Item ' + i, description: lorem, thumbnail: 'https://picsum.photos/800?hash=' + i, slug: 'qsd-'+i, tags: ['5f0e00e1a4dbfe3e0b5291d2'] }) - } + let organizationsData = organizations + .map(o => { + const version = isProposed ? o.get('proposedVersion'): o.get('publishedVersion') + return { + _id: o._id, + name: version.name, + description: version.descriptionShort, + thumbnail: version.thumbnail.key, + tags: version.tags === null ? 'tags_not_found' : version.tags, + slug: o.get('slugs')[o.get('slugs').length -1] + } + }) + // let lorem = "Dolore sit tempor et duo ipsum sit sed takimata, et magna voluptua ut sed justo eirmod. Sed tempor justo magna accusam aliquyam sea invidunt eos. Aliquyam accusam vero accusam sed" + // let organizationsData = [] + // for (var i = 0; i < 40; i++) { + // organizationsData.push({ _id: i, name: 'Item ' + i, description: lorem, thumbnail: 'https://picsum.photos/800?hash=' + i, slug: 'qsd-'+i, tags: ['5f0e00e1a4dbfe3e0b5291d2'] }) + // } res.render('home.twig', { isProposed, mediaBaseUrl: MediaService.getMediaBaseUrl(), diff --git a/src/models/Organization.ts b/src/models/Organization.ts index f45487a..63b27a8 100644 --- a/src/models/Organization.ts +++ b/src/models/Organization.ts @@ -26,6 +26,20 @@ class AllowedString extends mongoose.SchemaType { } } +const email = { + type: String, + trim: true, + lowercase: true, + unique: true, + validate: { + validator: function(v) { + return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(v); + }, + message: "Invalid email" + }, + required: [true, "Email required"] +} + // @ts-ignore mongoose.Schema.Types['AllowedString'] = AllowedString @@ -61,7 +75,7 @@ const OrganizationVersion = { phone: { type: String }, peoples: [{ name: { type: String }, - email: { type: String }, + email, phone: { type: String }, role: { type: String } }] @@ -85,7 +99,7 @@ const OrganizationVersion = { const Organization = new Schema({ adminName: { type: String, required: true }, - email: { type: String, required: true }, + email, token: { type: String, required: true }, slugs: [{ type: String }], // aliases system validationState: { diff --git a/yarn.lock b/yarn.lock index f2d4d3c..25621d4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -743,10 +743,10 @@ resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e" integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA== -"@types/papaparse@^5.0.4": - version "5.0.4" - resolved "https://registry.yarnpkg.com/@types/papaparse/-/papaparse-5.0.4.tgz#70792c74d9932bcc0bfa945ae7dacfef67f4ee57" - integrity sha512-jFv9NcRddMiW4+thmntwZ1AhvMDAX4+tAUDkWWbNcIzgqyjjkuSHOEUPoVh1/gqJTWfDOD1tvl+hSp88W3UtqA== +"@types/papaparse@^5.0.6": + version "5.0.6" + resolved "https://registry.yarnpkg.com/@types/papaparse/-/papaparse-5.0.6.tgz#4fee1a17dc24fed839d276c807f875bd653abbb4" + integrity sha512-fCQnycsOSviVLtxuydQ82IhTAv1lSPIA8zY3sYaRHI00lERsa7QSA616HsQZixqEF2asGXZag3hM13N8bm/jAw== dependencies: "@types/node" "*"