Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pateketrueke committed Aug 29, 2021
1 parent a6daf06 commit bb3e39e
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions lib/res.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,21 @@ function _fixRefs(refs, values) {
});
}

function _fileInfo(baseDir, data) {
function _fileInfo(baseDir, data, ex) {
/* istanbul ignore else */
if (!data) {
throw new Error('File data is missing');
}

/* istanbul ignore else */
if (Array.isArray(data)) {
return data.map(x => _fileInfo(baseDir, x));
// FIXME: additional fields on uploads
return data.map(x => _fileInfo(baseDir, x, ex));
}

delete ex.$upload;
return {
...ex,
mtime: (data.lastModifiedDate || new Date()).toGMTString(),
path: path.relative(baseDir, data.path),
name: data.name,
Expand Down Expand Up @@ -215,23 +218,23 @@ function _saveUploads(Model, field, payload, attachments, onUploadCallback) {
/* istanbul ignore else */
try {
if (payload.$upload) {
return _fileInfo(baseDir, attachments.files[payload.$upload]);
return _fileInfo(baseDir, attachments.files[payload.$upload], payload);
}
} catch (e) {
throw new Error(`Cannot retrieve ${field} '${payload.$upload}' (${e.message})`);
}

if (payload[Model.name]) {
if (payload[Model.name].$upload) {
const attachment = _fileInfo(baseDir, attachments.files[payload[Model.name].$upload]);
const attachment = _fileInfo(baseDir, attachments.files[payload[Model.name].$upload], payload[Model.name]);

tasks.push(_pushUpload(Model.name, schema, attachment.path, payload, attachment, onUploadCallback));
} else {
Object.keys(Model.associations).forEach(assoc => {
const data = payload[Model.name][assoc];

if (data && data.$upload && attachments.files[data.$upload]) {
const attachment = _fileInfo(baseDir, attachments.files[data.$upload]);
const attachment = _fileInfo(baseDir, attachments.files[data.$upload], data);

tasks.push(_pushUpload(assoc, schema[assoc], attachment.path, payload[Model.name], attachment, onUploadCallback));
}
Expand All @@ -244,7 +247,7 @@ function _saveUploads(Model, field, payload, attachments, onUploadCallback) {
&& payload[prop].$upload
&& attachments.files[payload[prop].$upload]
) {
const attachment = _fileInfo(baseDir, attachments.files[payload[prop].$upload]);
const attachment = _fileInfo(baseDir, attachments.files[payload[prop].$upload], payload[prop]);

tasks.push(_pushUpload(prop, schema[prop], attachment.path, payload, attachment, onUploadCallback));
}
Expand Down Expand Up @@ -789,7 +792,7 @@ module.exports = (conn, options, modelName) => {
function build(payload, isUpdate, _options) {
_options = _options || {};
_options.logging = options.logging || _options.logging;
_options.where = Object.assign({}, options.where, _options.where, _where);
_options.where = Object.assign({}, _options.where, _where);

let _payload;
let _tasks;
Expand All @@ -804,9 +807,7 @@ module.exports = (conn, options, modelName) => {
.then(() => _tasks.before.reduce((prev, cur) => prev.then(() => cur(_opts, isUpdate)), Promise.resolve()))
.then(() => model[isUpdate ? 'update' : 'create'](_payload, _opts))
.then(row => {
let pk = !isUpdate
? row[0]
: null;
let pk;

/* istanbul ignore else */
if (!Array.isArray(row)) {
Expand All @@ -820,16 +821,20 @@ module.exports = (conn, options, modelName) => {

_payload[model.primaryKeyAttribute] = _payload[model.primaryKeyAttribute] || pk;

return _tasks.after.reduce((prev, cur) => prev.then(() => cur(pk, _opts, isUpdate)), Promise.resolve());
/* istanbul ignore else */
if (!isUpdate) {
row = [row, _payload];
}

return _tasks.after.reduce((prev, cur) => prev.then(() => cur(pk, _opts, isUpdate)), Promise.resolve()).then(() => row);
})
.then(() => _payload)
.catch(err);
}

function fetch(method, _options) {
_options = _options || {};
_options.logging = options.logging || _options.logging;
_options.where = Object.assign({}, options.where, _options.where, _where);
_options.where = Object.assign({}, _options.where, _where);

const _pk = model.primaryKeyAttribute;
const _opts = _getOpts(model, _props, method, _options);
Expand Down

0 comments on commit bb3e39e

Please sign in to comment.