fix: minor fixes and add csv import support
This commit is contained in:
parent
289e7839c6
commit
fefbf15f5e
6 changed files with 73 additions and 32 deletions
|
|
@ -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))
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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: {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue