修正了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) { function getUserRelationsByActions(params, context) {
return tslib_1.__awaiter(this, void 0, void 0, function () { 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; var _this = this;
return tslib_1.__generator(this, function (_a) { return tslib_1.__generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
@ -2104,112 +2104,127 @@ function getUserRelationsByActions(params, context) {
}, { dontCollect: true })]; }, { dontCollect: true })];
case 1: case 1:
actionAuths = _a.sent(); actionAuths = _a.sent();
makeRelationIterator = function (path, relationIds) { getUserRelations = function (urAuths) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var paths = path.split('.'); var makeRelationIterator, urAuthDict, urAuthGroups, userRelations;
var makeIter = function (e, idx) { var _this = this;
var _a, _b, _c; return tslib_1.__generator(this, function (_a) {
if (idx === paths.length) { switch (_a.label) {
return { case 0:
projection: { makeRelationIterator = function (path, relationIds) {
id: 1, var paths = path.split('.');
userRelation$entity: { var makeIter = function (e, idx) {
$entity: 'userRelation', var _a, _b, _c;
data: { if (idx === paths.length) {
id: 1, return {
relationId: 1, projection: {
relation: { id: 1,
id: 1, userRelation$entity: {
name: 1, $entity: 'userRelation',
}, data: {
entity: 1, id: 1,
entityId: 1, relationId: 1,
}, relation: {
filter: { id: 1,
relationId: { name: 1,
$in: relationIds, },
}, entity: 1,
}, entityId: 1,
} userId: 1,
}, },
getData: function (d) { filter: {
return d.userRelation$entity; 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) { urAuths2 = actionAuths.filter(function (ele) { return !!ele.relationId; } // 有relation说明通过userRelation关联
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关联
); );
urAuthDict = (0, lodash_1.groupBy)(urAuths, 'path'); return [4 /*yield*/, getUserRelations(urAuths2)];
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 2: case 2:
userRelations = _a.sent(); userRelations = _a.sent();
// todo还有直接的user
return [2 /*return*/, { return [2 /*return*/, {
userRelations: userRelations.flat(), userRelations: userRelations,
}]; }];
} }
}); });

View File

@ -2538,110 +2538,118 @@ export class RelationAuth<ED extends EntityDict & BaseEntityDict>{
filter: actionAuthfilter, filter: actionAuthfilter,
}, { dontCollect: true }); }, { dontCollect: true });
const makeRelationIterator = (path: string, relationIds: string[]) => { const getUserRelations = async (urAuths: Partial<ED['actionAuth']['Schema']>[]) => {
const paths = path.split('.'); const makeRelationIterator = (path: string, relationIds: string[]) => {
const paths = path.split('.');
const makeIter = (e: keyof ED, idx: number): {
projection: ED[keyof ED]['Selection']['data']; const makeIter = (e: keyof ED, idx: number): {
getData: (d: Partial<ED[keyof ED]['Schema']>) => any; projection: ED[keyof ED]['Selection']['data'];
} => { getData: (d: Partial<ED[keyof ED]['Schema']>) => any;
if (idx === paths.length) { } => {
return { if (idx === paths.length) {
projection: { return {
id: 1, projection: {
userRelation$entity: { id: 1,
$entity: 'userRelation', userRelation$entity: {
data: { $entity: 'userRelation',
id: 1, data: {
relationId: 1,
relation: {
id: 1, id: 1,
name: 1, relationId: 1,
relation: {
id: 1,
name: 1,
},
entity: 1,
entityId: 1,
userId: 1,
}, },
entity: 1, filter: {
entityId: 1, relationId: {
}, $in: relationIds,
filter: { },
relationId: {
$in: relationIds,
}, },
}, } as ED['userRelation']['Selection']
} as ED['userRelation']['Selection'] } as ED[keyof ED]['Selection']['data'],
} as ED[keyof ED]['Selection']['data'], getData: (d: Partial<ED[keyof ED]['Schema']>) => {
getData: (d: Partial<ED[keyof ED]['Schema']>) => { return d.userRelation$entity;
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,
}, },
}, };
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关联 ele => !!ele.relationId // 有relation说明通过userRelation关联
); );
// 相同的path可以groupBy掉 const userRelations = await getUserRelations(urAuths2);
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'][];
}
));
// todo还有直接的user
return { return {
userRelations: userRelations.flat(), userRelations,
}; };
} }