修正了getUserRelationsByActions中的两个小的逻辑错误

This commit is contained in:
Xu Chang 2023-07-26 20:22:27 +08:00
parent 3ef11c640a
commit d5080e92b8
2 changed files with 216 additions and 193 deletions

View File

@ -2067,7 +2067,7 @@ exports.RelationAuth = RelationAuth;
*/
function getUserRelationsByActions(params, context) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var entity, filter, actions, overlap, actionAuthfilter, actionAuths, makeRelationIterator, urAuths, urAuthDict, urAuthGroups, userRelations;
var entity, filter, actions, overlap, actionAuthfilter, actionAuths, getUserRelations, urAuths2, userRelations;
var _this = this;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
@ -2104,112 +2104,127 @@ function getUserRelationsByActions(params, context) {
}, { dontCollect: true })];
case 1:
actionAuths = _a.sent();
makeRelationIterator = function (path, relationIds) {
var paths = path.split('.');
var makeIter = function (e, idx) {
var _a, _b, _c;
if (idx === paths.length) {
return {
projection: {
id: 1,
userRelation$entity: {
$entity: 'userRelation',
data: {
id: 1,
relationId: 1,
relation: {
id: 1,
name: 1,
},
entity: 1,
entityId: 1,
},
filter: {
relationId: {
$in: relationIds,
},
},
}
},
getData: function (d) {
return d.userRelation$entity;
},
};
getUserRelations = function (urAuths) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var makeRelationIterator, urAuthDict, urAuthGroups, userRelations;
var _this = this;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
makeRelationIterator = function (path, relationIds) {
var paths = path.split('.');
var makeIter = function (e, idx) {
var _a, _b, _c;
if (idx === paths.length) {
return {
projection: {
id: 1,
userRelation$entity: {
$entity: 'userRelation',
data: {
id: 1,
relationId: 1,
relation: {
id: 1,
name: 1,
},
entity: 1,
entityId: 1,
userId: 1,
},
filter: {
relationId: {
$in: relationIds,
},
},
}
},
getData: function (d) {
return d.userRelation$entity;
},
};
}
var attr = paths[idx];
var rel = (0, relation_1.judgeRelation)(context.getSchema(), e, attr);
if (rel === 2) {
var _d = makeIter(attr, idx + 1), projection = _d.projection, getData_1 = _d.getData;
return {
projection: (_a = {
id: 1
},
_a[attr] = projection,
_a),
getData: function (d) { return d[attr] && getData_1(d[attr]); },
};
}
else if (typeof rel === 'string') {
var _e = makeIter(rel, idx + 1), projection = _e.projection, getData_2 = _e.getData;
return {
projection: (_b = {
id: 1
},
_b[attr] = projection,
_b),
getData: function (d) { return d[attr] && getData_2(d[attr]); },
};
}
else {
(0, assert_1.default)(rel instanceof Array);
var _f = tslib_1.__read(rel, 2), e2 = _f[0], fk = _f[1];
var _g = makeIter(e2, idx + 1), projection = _g.projection, getData_3 = _g.getData;
return {
projection: (_c = {
id: 1
},
_c[attr] = {
$entity: e2,
data: projection,
},
_c),
getData: function (d) { return d[attr] && d[attr].map(function (ele) { return getData_3(ele); }); },
};
}
};
return makeIter(entity, 0);
};
urAuthDict = (0, lodash_1.groupBy)(urAuths, 'path');
urAuthGroups = Object.keys(urAuthDict).map(function (ele) { return ({
path: ele,
relationIds: urAuthDict[ele].map(function (ele) { return ele.relationId; })
}); });
return [4 /*yield*/, Promise.all(urAuthGroups.map(function (_a) {
var path = _a.path, relationIds = _a.relationIds;
return tslib_1.__awaiter(_this, void 0, void 0, function () {
var _b, projection, getData, rows, urs;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
_b = makeRelationIterator(path, relationIds), projection = _b.projection, getData = _b.getData;
return [4 /*yield*/, context.select(entity, {
data: projection,
filter: filter,
}, { dontCollect: true })];
case 1:
rows = _c.sent();
urs = rows.map(function (ele) { return getData(ele); }).flat().filter(function (ele) { return !!ele; });
return [2 /*return*/, urs];
}
});
});
}))];
case 1:
userRelations = _a.sent();
return [2 /*return*/, userRelations.flat()];
}
var attr = path[idx];
var rel = (0, relation_1.judgeRelation)(context.getSchema(), e, attr);
if (rel === 2) {
var _d = makeIter(attr, idx + 1), projection = _d.projection, getData_1 = _d.getData;
return {
projection: (_a = {
id: 1
},
_a[attr] = projection,
_a),
getData: function (d) { return d[attr] && getData_1(d[attr]); },
};
}
else if (typeof rel === 'string') {
var _e = makeIter(rel, idx + 1), projection = _e.projection, getData_2 = _e.getData;
return {
projection: (_b = {
id: 1
},
_b[attr] = projection,
_b),
getData: function (d) { return d[attr] && getData_2(d[attr]); },
};
}
else {
(0, assert_1.default)(rel instanceof Array);
var _f = tslib_1.__read(rel, 2), e2 = _f[0], fk = _f[1];
var _g = makeIter(e2, idx + 1), projection = _g.projection, getData_3 = _g.getData;
return {
projection: (_c = {
id: 1
},
_c[attr] = {
$entity: e2,
data: projection,
},
_c),
getData: function (d) { return d[attr] && d[attr].map(function (ele) { return getData_3(ele); }); },
};
}
};
return makeIter(entity, 0);
};
urAuths = actionAuths.filter(function (ele) { return !!ele.relationId; } // 有relation说明通过userRelation关联
});
}); };
urAuths2 = actionAuths.filter(function (ele) { return !!ele.relationId; } // 有relation说明通过userRelation关联
);
urAuthDict = (0, lodash_1.groupBy)(urAuths, 'path');
urAuthGroups = Object.keys(urAuthDict).map(function (ele) { return ({
path: ele,
relationIds: urAuthDict[ele].map(function (ele) { return ele.relationId; })
}); });
return [4 /*yield*/, Promise.all(urAuthGroups.map(function (_a) {
var path = _a.path, relationIds = _a.relationIds;
return tslib_1.__awaiter(_this, void 0, void 0, function () {
var _b, projection, getData, rows, urs;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
_b = makeRelationIterator(path, relationIds), projection = _b.projection, getData = _b.getData;
return [4 /*yield*/, context.select(entity, {
data: projection,
filter: filter,
}, { dontCollect: true })];
case 1:
rows = _c.sent();
urs = rows.map(function (ele) { return getData(ele); }).flat().filter(function (ele) { return !!ele; });
return [2 /*return*/, urs];
}
});
});
}))];
return [4 /*yield*/, getUserRelations(urAuths2)];
case 2:
userRelations = _a.sent();
// todo还有直接的user
return [2 /*return*/, {
userRelations: userRelations.flat(),
userRelations: userRelations,
}];
}
});

View File

@ -2538,110 +2538,118 @@ export class RelationAuth<ED extends EntityDict & BaseEntityDict>{
filter: actionAuthfilter,
}, { dontCollect: true });
const makeRelationIterator = (path: string, relationIds: string[]) => {
const paths = path.split('.');
const makeIter = (e: keyof ED, idx: number): {
projection: ED[keyof ED]['Selection']['data'];
getData: (d: Partial<ED[keyof ED]['Schema']>) => any;
} => {
if (idx === paths.length) {
return {
projection: {
id: 1,
userRelation$entity: {
$entity: 'userRelation',
data: {
id: 1,
relationId: 1,
relation: {
const getUserRelations = async (urAuths: Partial<ED['actionAuth']['Schema']>[]) => {
const makeRelationIterator = (path: string, relationIds: string[]) => {
const paths = path.split('.');
const makeIter = (e: keyof ED, idx: number): {
projection: ED[keyof ED]['Selection']['data'];
getData: (d: Partial<ED[keyof ED]['Schema']>) => any;
} => {
if (idx === paths.length) {
return {
projection: {
id: 1,
userRelation$entity: {
$entity: 'userRelation',
data: {
id: 1,
name: 1,
relationId: 1,
relation: {
id: 1,
name: 1,
},
entity: 1,
entityId: 1,
userId: 1,
},
entity: 1,
entityId: 1,
},
filter: {
relationId: {
$in: relationIds,
filter: {
relationId: {
$in: relationIds,
},
},
},
} as ED['userRelation']['Selection']
} as ED[keyof ED]['Selection']['data'],
getData: (d: Partial<ED[keyof ED]['Schema']>) => {
return d.userRelation$entity;
},
};
}
const attr = path[idx];
const rel = judgeRelation(context.getSchema(), e, attr);
if (rel === 2) {
const { projection, getData } = makeIter(attr, idx + 1);
return {
projection: {
id: 1,
[attr]: projection,
},
getData: (d) => d[attr] && getData(d[attr]!),
};
}
else if (typeof rel === 'string') {
const { projection, getData } = makeIter(rel, idx + 1);
return {
projection: {
id: 1,
[attr]: projection,
},
getData: (d) => d[attr] && getData(d[attr]!),
};
}
else {
assert(rel instanceof Array);
const [e2, fk] = rel;
const { projection, getData } = makeIter(e2, idx + 1);
return {
projection: {
id: 1,
[attr]: {
$entity: e2,
data: projection,
} as ED['userRelation']['Selection']
} as ED[keyof ED]['Selection']['data'],
getData: (d: Partial<ED[keyof ED]['Schema']>) => {
return d.userRelation$entity;
},
},
getData: (d) => d[attr] && d[attr]!.map((ele: any) => getData(ele)),
};
}
}
const attr = paths[idx];
const rel = judgeRelation(context.getSchema(), e, attr);
if (rel === 2) {
const { projection, getData } = makeIter(attr, idx + 1);
return {
projection: {
id: 1,
[attr]: projection,
},
getData: (d) => d[attr] && getData(d[attr]!),
};
}
else if (typeof rel === 'string') {
const { projection, getData } = makeIter(rel, idx + 1);
return {
projection: {
id: 1,
[attr]: projection,
},
getData: (d) => d[attr] && getData(d[attr]!),
};
}
else {
assert(rel instanceof Array);
const [e2, fk] = rel;
const { projection, getData } = makeIter(e2, idx + 1);
return {
projection: {
id: 1,
[attr]: {
$entity: e2,
data: projection,
},
},
getData: (d) => d[attr] && d[attr]!.map((ele: any) => getData(ele)),
}
}
};
return makeIter(entity, 0);
};
// 相同的path可以groupBy掉
const urAuthDict = groupBy(urAuths, 'path');
const urAuthGroups = Object.keys(urAuthDict).map(
ele => ({
path: ele,
relationIds: urAuthDict[ele].map(ele => ele.relationId!)
})
);
const userRelations = await Promise.all(urAuthGroups.map(
async ({ path, relationIds }) => {
const { projection, getData } = makeRelationIterator(path, relationIds);
const rows = await context.select(entity, {
data: projection,
filter,
}, { dontCollect: true });
const urs = rows.map(ele => getData(ele)).flat().filter(ele => !!ele);
return urs as ED['userRelation']['Schema'][];
}
));
return makeIter(entity, 0);
}
return userRelations.flat();
};
const urAuths = actionAuths.filter(
const urAuths2 = actionAuths.filter(
ele => !!ele.relationId // 有relation说明通过userRelation关联
);
// 相同的path可以groupBy掉
const urAuthDict = groupBy(urAuths, 'path');
const urAuthGroups = Object.keys(urAuthDict).map(
ele => ({
path: ele,
relationIds: urAuthDict[ele].map(ele => ele.relationId!)
})
);
const userRelations = await Promise.all(urAuthGroups.map(
async ({ path, relationIds }) => {
const { projection, getData } = makeRelationIterator(path, relationIds);
const rows = await context.select(entity, {
data: projection,
filter,
}, { dontCollect: true });
const urs = rows.map(ele => getData(ele)).flat().filter(ele => !!ele);
return urs as ED['userRelation']['Schema'][];
}
));
const userRelations = await getUserRelations(urAuths2);
// todo还有直接的user
return {
userRelations: userRelations.flat(),
userRelations,
};
}