mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 08:47:20 +00:00
fix: values to filter with emtpy values (#4319)
* fix: values to filter with emtpy values * chore: test * fix: values to filter * fix: test
This commit is contained in:
parent
afa9f3faa4
commit
8b3b9b3b26
@ -50,4 +50,49 @@ describe('update or create', () => {
|
||||
expect(post).not.toBeNull();
|
||||
expect(post['title']).toEqual('t1');
|
||||
});
|
||||
|
||||
test('update or create with empty values', async () => {
|
||||
await Post.repository.create({ values: { title: 't1' } });
|
||||
|
||||
const response = await app
|
||||
.agent()
|
||||
.resource('posts')
|
||||
.updateOrCreate({
|
||||
values: {},
|
||||
filterKeys: ['title'],
|
||||
});
|
||||
|
||||
expect(response.statusCode).toEqual(200);
|
||||
const post = await Post.repository.findOne({
|
||||
filter: {
|
||||
'title.$empty': true,
|
||||
},
|
||||
});
|
||||
|
||||
expect(post).not.toBeNull();
|
||||
|
||||
await app
|
||||
.agent()
|
||||
.resource('posts')
|
||||
.updateOrCreate({
|
||||
values: {},
|
||||
filterKeys: ['title'],
|
||||
});
|
||||
|
||||
expect(
|
||||
await Post.repository.count({
|
||||
filter: {
|
||||
'title.$empty': true,
|
||||
},
|
||||
}),
|
||||
).toEqual(1);
|
||||
|
||||
expect(
|
||||
await Post.repository.count({
|
||||
filter: {
|
||||
title: 't1',
|
||||
},
|
||||
}),
|
||||
).toEqual(1);
|
||||
});
|
||||
});
|
||||
|
@ -191,8 +191,11 @@ describe('create', () => {
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(u1.name).toEqual('u1');
|
||||
|
||||
const group = await u1.get('group');
|
||||
|
||||
expect(group.name).toEqual('g1');
|
||||
|
||||
const u2 = await User.repository.firstOrCreate({
|
||||
|
@ -242,11 +242,8 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
|
||||
this.model = collection.model;
|
||||
}
|
||||
|
||||
public static valuesToFilter(values: Values, filterKeys: Array<string>) {
|
||||
const filterAnd = [];
|
||||
const flattedValues = flatten(values);
|
||||
|
||||
const keyWithOutArrayIndex = (key) => {
|
||||
public static valuesToFilter(values: Values = {}, filterKeys: Array<string>) {
|
||||
const removeArrayIndexInKey = (key) => {
|
||||
const chunks = key.split('.');
|
||||
return chunks
|
||||
.filter((chunk) => {
|
||||
@ -255,29 +252,36 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
|
||||
.join('.');
|
||||
};
|
||||
|
||||
const filterAnd = [];
|
||||
const flattedValues = flatten(values);
|
||||
const flattedValuesObject = {};
|
||||
|
||||
for (const key in flattedValues) {
|
||||
const keyWithoutArrayIndex = removeArrayIndexInKey(key);
|
||||
if (flattedValuesObject[keyWithoutArrayIndex]) {
|
||||
if (!Array.isArray(flattedValuesObject[keyWithoutArrayIndex])) {
|
||||
flattedValuesObject[keyWithoutArrayIndex] = [flattedValuesObject[keyWithoutArrayIndex]];
|
||||
}
|
||||
|
||||
flattedValuesObject[keyWithoutArrayIndex].push(flattedValues[key]);
|
||||
} else {
|
||||
flattedValuesObject[keyWithoutArrayIndex] = [flattedValues[key]];
|
||||
}
|
||||
}
|
||||
|
||||
for (const filterKey of filterKeys) {
|
||||
let filterValue;
|
||||
|
||||
for (const flattedKey of Object.keys(flattedValues)) {
|
||||
const flattedKeyWithoutIndex = keyWithOutArrayIndex(flattedKey);
|
||||
|
||||
if (flattedKeyWithoutIndex === filterKey) {
|
||||
if (filterValue) {
|
||||
if (Array.isArray(filterValue)) {
|
||||
filterValue.push(flattedValues[flattedKey]);
|
||||
} else {
|
||||
filterValue = [filterValue, flattedValues[flattedKey]];
|
||||
}
|
||||
} else {
|
||||
filterValue = flattedValues[flattedKey];
|
||||
}
|
||||
}
|
||||
}
|
||||
const filterValue = flattedValuesObject[filterKey]
|
||||
? flattedValuesObject[filterKey]
|
||||
: lodash.get(values, filterKey);
|
||||
|
||||
if (filterValue) {
|
||||
filterAnd.push({
|
||||
[filterKey]: filterValue,
|
||||
});
|
||||
} else {
|
||||
filterAnd.push({
|
||||
[filterKey]: null,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user