Beginning of work on templates.

This commit is contained in:
Tomas Bures 2018-02-13 23:50:13 +01:00
parent 47b8d80c22
commit 508d6b3b2f
40 changed files with 1685 additions and 1031 deletions

View file

@ -630,8 +630,14 @@ async function forHbs(context, listId, subscription) { // assumes grouped subscr
return customFields;
}
// Converts subscription data received via POST request from subscription form or via subscribe request to API v1 to subscription structure supported by subscriptions model.
async function fromPost(context, listId, data) { // assumes grouped subscription
// This is to handle option values from API v1
function isSelected(value) {
return ['false', 'no', '0', ''].indexOf((value || '').toString().trim().toLowerCase()) >= 0 ? false : true;
}
const flds = await listGrouped(context, listId);
const subscription = {};
@ -650,7 +656,10 @@ async function fromPost(context, listId, data) { // assumes grouped subscription
for (const optCol in fld.groupedOptions) {
const opt = fld.groupedOptions[optCol];
if (data[fld.key] === opt.key) {
// This handles two different formats for grouped dropdowns and radios.
// The first part of the condition handles the POST requests from the subscription form, while the
// second part handles the subscribe request to API v1
if (data[fld.key] === opt.key || isSelected(data[opt.key])) {
value = opt.column
}
}
@ -660,7 +669,7 @@ async function fromPost(context, listId, data) { // assumes grouped subscription
for (const optCol in fld.groupedOptions) {
const opt = fld.groupedOptions[optCol];
if (data[opt.key]) {
if (isSelected(data[opt.key])) {
value.push(opt.column);
}
}

155
models/files.js Normal file
View file

@ -0,0 +1,155 @@
'use strict';
const knex = require('../lib/knex');
const { enforce } = require('../lib/helpers');
const dtHelpers = require('../lib/dt-helpers');
const shares = require('./shares');
const fs = require('fs-extra-promise');
const path = require('path');
const filesDir = path.join(__dirname, '..', 'files');
const permittedTypes = new Set(['template']);
function getFilePath(type, entityId, filename) {
return path.join(path.join(filesDir, type, id.toString()), filename);
}
function getFilesTable(type) {
return 'files_' + type;
}
async function listFilesDTAjax(context, type, entityId, params) {
enforce(permittedTypes.has(type));
await shares.enforceEntityPermission(context, type, entityId, 'edit');
return await dtHelpers.ajaxList(
params,
builder => builder.from(getFilesTable(type)).where({entity: entityId}),
['id', 'originalname', 'size', 'created']
);
}
async function getFileById(context, type, id) {
enforce(permittedTypes.has(type));
const file = await knex.transaction(async tx => {
const file = await knex(getFilesTable(type)).where('id', id).first();
await shares.enforceEntityPermissionTx(tx, context, type, file.entity, 'edit');
return file;
});
return {
mimetype: file.mimetype,
name: file.originalname,
path: getFilePath(type, file.entity, file.filename)
};
}
async function getFileByName(context, type, entityId, name) {
enforce(permittedTypes.has(type));
const file = await knex.transaction(async tx => {
await shares.enforceEntityPermissionTx(tx, context, type, entityId, 'view');
const file = await knex(getFilesTable(type)).where({entity: entityId, originalname: name}).first();
return file;
});
return {
mimetype: file.mimetype,
name: file.originalname,
path: getFilePath(type, file.entity, file.filename)
};
}
async function createFiles(context, type, entityId, files) {
enforce(permittedTypes.has(type));
if (files.length == 0) {
// No files uploaded
return {uploaded: 0};
}
const originalNameSet = new Set();
const fileEntities = new Array();
const filesToMove = new Array();
const ignoredFiles = new Array();
// Create entities for files
for (const file of files) {
if (originalNameSet.has(file.originalname)) {
// The file has an original name same as another file
ignoredFiles.push(file);
} else {
originalNameSet.add(file.originalname);
const fileEntity = {
entity: entityId,
filename: file.filename,
originalname: file.originalname,
mimetype: file.mimetype,
encoding: file.encoding,
size: file.size
};
fileEntities.push(fileEntity);
filesToMove.push(file);
}
}
const originalNameArray = Array.from(originalNameSet);
const removedFiles = await knex.transaction(async tx => {
await shares.enforceEntityPermissionTx(tx, context, type, entityId, 'edit');
const removedFiles = await knex(getFilesTable(type)).where('entity', entityId).whereIn('originalname', originalNameArray).select(['filename', 'originalname']);
await knex(getFilesTable(type)).where('entity', entityId).whereIn('originalname', originalNameArray).del();
if(fileEntities){
await knex(getFilesTable(type)).insert(fileEntities);
}
return removedFiles;
});
const removedNameSet = new Set();
// Move new files from upload directory to files directory
for(const file of filesToMove){
const filePath = getFilePath(entityId, file.filename);
// The names should be unique, so overwrite is disabled
// The directory is created if it does not exist
// Empty options argument is passed, otherwise fails
await fs.move(file.path, filePath, {});
}
// Remove replaced files from files directory
for(const file of removedFiles){
removedNameSet.add(file.originalname);
const filePath = getFilePath(type, entityId, file.filename);
await fs.remove(filePath);
}
// Remove ignored files from upload directory
for(const file of ignoredFiles){
await fs.remove(file.path);
}
return {
uploaded: files.length,
added: fileEntities.length - removedNameSet.size,
replaced: removedFiles.length,
ignored: ignoredFiles.length
};
}
async function removeFile(context, type, id) {
const file = await knex.transaction(async tx => {
const file = await knex(getFilesTable(type)).where('id', id).select('entity', 'filename').first();
await shares.enforceEntityPermissionTx(tx, context, type, file.entity, 'edit');
await tx(getFilesTable(type)).where('id', id).del();
return {filename: file.filename, entity: file.entity};
});
const filePath = getFilePath(type, file.entity, file.filename);
await fs.remove(filePath);
}
module.exports = {
listFilesDTAjax,
getFileById,
getFileByName,
createFiles,
removeFile
};

View file

@ -400,7 +400,7 @@ async function _validateAndPreprocess(tx, listId, groupedFieldsMap, entity, meta
}
const existingWithKey = await existingWithKeyQuery.first();
if (existingWithKey) {
if (meta && meta.replaceOfUnsubscribedAllowed && existingWithKey.status === SubscriptionStatus.UNSUBSCRIBED) {
if (meta && (meta.updateAllowed || meta.updateOfUnsubscribedAllowed && existingWithKey.status === SubscriptionStatus.UNSUBSCRIBED)) {
meta.update = true;
meta.existing = existingWithKey;
} else {
@ -476,7 +476,7 @@ async function create(context, listId, entity, meta /* meta is provided when cal
filteredEntity.opt_in_country = meta && meta.country;
filteredEntity.imported = meta && !!meta.imported;
if (meta && meta.update) {
if (meta && meta.update) { // meta.update is set by _validateAndPreprocess
await _update(tx, listId, meta.existing, filteredEntity);
meta.cid = meta.existing.cid; // The cid is needed by /confirm/subscribe/:cid
return meta.existing.id;

91
models/templates.js Normal file
View file

@ -0,0 +1,91 @@
'use strict';
const knex = require('../lib/knex');
const hasher = require('node-object-hash')();
const { enforce, filterObject } = require('../lib/helpers');
const dtHelpers = require('../lib/dt-helpers');
const interoperableErrors = require('../shared/interoperable-errors');
const namespaceHelpers = require('../lib/namespace-helpers');
const shares = require('./shares');
const reports = require('./reports');
const allowedKeys = new Set(['name', 'description', 'type', 'data', 'html', 'text', 'namespace']);
function hash(entity) {
return hasher.hash(filterObject(entity, allowedKeys));
}
async function getById(context, id) {
return await knex.transaction(async tx => {
await shares.enforceEntityPermissionTx(tx, context, 'template', id, 'view');
const entity = await tx('templates').where('id', id).first();
entity.permissions = await shares.getPermissionsTx(tx, context, 'template', id);
return entity;
});
}
async function listDTAjax(context, params) {
return await dtHelpers.ajaxListWithPermissions(
context,
[{ entityTypeId: 'template', requiredOperations: ['view'] }],
params,
builder => builder.from('templates').innerJoin('namespaces', 'namespaces.id', 'templates.namespace'),
[ 'templates.id', 'templates.name', 'templates.description', 'templates.type', 'templates.created', 'namespaces.name' ]
);
}
async function create(context, entity) {
return await knex.transaction(async tx => {
await shares.enforceEntityPermissionTx(tx, context, 'namespace', entity.namespace, 'createTemplate');
await namespaceHelpers.validateEntity(tx, entity);
const ids = await tx('templates').insert(filterObject(entity, allowedKeys));
const id = ids[0];
await shares.rebuildPermissionsTx(tx, { entityTypeId: 'template', entityId: id });
return id;
});
}
async function updateWithConsistencyCheck(context, entity) {
await knex.transaction(async tx => {
await shares.enforceEntityPermissionTx(tx, context, 'template', entity.id, 'edit');
const existing = await tx('templates').where('id', entity.id).first();
if (!existing) {
throw new interoperableErrors.NotFoundError();
}
const existingHash = hash(existing);
if (existingHash !== entity.originalHash) {
throw new interoperableErrors.ChangedError();
}
await namespaceHelpers.validateEntity(tx, entity);
await namespaceHelpers.validateMove(context, entity, existing, 'template', 'createTemplate', 'delete');
await tx('templates').where('id', entity.id).update(filterObject(entity, allowedKeys));
await shares.rebuildPermissionsTx(tx, { entityTypeId: 'template', entityId: entity.id });
});
}
async function remove(context, id) {
await knex.transaction(async tx => {
await shares.enforceEntityPermissionTx(tx, context, 'template', id, 'delete');
await reports.removeAllByReportTemplateIdTx(tx, context, id);
await tx('templates').where('id', id).del();
});
}
module.exports = {
hash,
getById,
listDTAjax,
create,
updateWithConsistencyCheck,
remove
};