Skip to content

feat: able to unset website field #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
8 changes: 8 additions & 0 deletions schemas/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,14 @@
},
"categories": {
"$ref": "common#/definitions/categories"
},
"$remove": {
"type": "array",
"items": {
"type": "string",
"minLength": 1
},
"minItems": 1
}
}
},
Expand Down
24 changes: 24 additions & 0 deletions src/actions/update.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { ActionTransport } = require('@microfleet/plugin-router');
const Promise = require('bluebird');
const { HttpStatusError } = require('common-errors');
const ld = require('lodash');
const handlePipeline = require('../utils/pipeline-error');
const fetchData = require('../utils/fetch-data');
const isProcessed = require('../utils/is-processed');
Expand Down Expand Up @@ -81,6 +82,27 @@ function preProcessMetadata(data) {
return data;
}

/**
* Process metadata remove operation
* @param {Object} pipeline
* @param {Object} meta
*/
function handleRemoveFromMeta(pipeline, key, meta, data) {
const { $remove } = meta;

if ($remove && $remove.length > 0) {
const existsData = ld.pick(data, $remove);
const esistsKeys = ld.keys(existsData);

pipeline.hdel(key, esistsKeys);

$remove.forEach((removeKey) => {
delete meta[removeKey];
});
delete meta.$remove;
}
}

async function updateMeta(lock, ctx, params) {
const { uploadId, username, directOnly, immutable, includeReferences } = params;
const { redis } = ctx;
Expand Down Expand Up @@ -151,6 +173,8 @@ async function updateMeta(lock, ctx, params) {
delete meta[FILES_ALIAS_FIELD]; // <-- this field is empty at this point
}

handleRemoveFromMeta(pipeline, key, meta, data);

if (hasOwnProperty.call(meta, FILES_TAGS_FIELD) && data[FILES_TAGS_FIELD]) {
// @todo migrate all tags in files data to lowercase and then remove this tag.toLowerCase()
for (const tag of data[FILES_TAGS_FIELD].values()) {
Expand Down
90 changes: 84 additions & 6 deletions test/suites/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,73 @@ describe('update suite', function suite() {

assert.strictEqual(fileInfo.file.description, 'foo', 'Description should be trimmed');
});

it('remove description', async function test() {
const { uploadId } = this.response;

await this.send({
uploadId,
username,
meta: {
description: 'some-new-description',
$remove: ['description'],
},
}, 45000);

const fileInfo = await getInfo.call(this, {
filename: uploadId,
username,
});

assert.strictEqual(fileInfo.file.description, undefined, 'Description should be not exists');
});
});

describe('update website', function updateWebsite() {
it('failed to update wrong url', async function test() {
meta.website = 'wrong-url';

await assert.rejects(this.send({ uploadId: this.response.uploadId, username, meta }, 45000), {
statusCode: 400,
});
});

it('able to update website any website', async function test() {
const { uploadId } = this.response;
meta.website = faker.image.imageUrl();

await this.send({
uploadId,
username,
meta,
}, 45000);

const fileInfo = await getInfo.call(this, {
filename: uploadId,
username,
});

assert.strictEqual(fileInfo.file.website, meta.website, 'Website should be equal with update data');
});

it('able to unset website passing an empty string', async function test() {
const { uploadId } = this.response;

await this.send({
uploadId,
username,
meta: {
$remove: ['website'],
},
}, 45000);

const fileInfo = await getInfo.call(this, {
filename: uploadId,
username,
});

assert.strictEqual(fileInfo.file.website, undefined, 'Website must not exists');
});
});

describe('playerSettings', function playerSettingsSuite() {
Expand Down Expand Up @@ -530,10 +597,15 @@ describe('update suite', function suite() {
});
});

it('able to unset backgroundImage passing an empty string', function test() {
meta.backgroundImage = '';
return this
.send({ uploadId: this.response.uploadId, username, meta }, 45000)
it('remove backgroundImage', async function test() {
await this
.send({
uploadId: this.response.uploadId,
username,
meta: {
$remove: ['backgroundImage'],
},
}, 45000)
.then(async (result) => {
assert.equal(result, true);

Expand All @@ -542,7 +614,7 @@ describe('update suite', function suite() {
username,
});

assert.equal(verifyResult.file.backgroundImage, meta.backgroundImage);
assert.equal(verifyResult.file.backgroundImage, undefined, 'backgroundImage should be not exists');
});
});

Expand All @@ -559,7 +631,13 @@ describe('update suite', function suite() {
let modelWithReference;

before('upload-file', async function uploadFile() {
meta.backgroundImage = '';
await initAndUpload(backgroundData, false).call(this)
.then(downloadFile.bind(this))
.then(({ urls }) => {
[meta.backgroundImage] = urls;
return null;
});

const uploaded = await initAndUpload({
...modelData,
message: {
Expand Down