重写了getUserRelationsByActions

This commit is contained in:
Xu Chang 2023-07-26 20:08:11 +08:00
parent 07d79cd731
commit 3ef11c640a
3 changed files with 238 additions and 49 deletions

View File

@ -109,7 +109,9 @@ export declare class RelationAuth<ED extends EntityDict & BaseEntityDict> {
*/ */
export declare function getUserRelationsByActions<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>>(params: { export declare function getUserRelationsByActions<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>>(params: {
entity: T; entity: T;
entityId: string; filter: ED[T]['Selection']['filter'];
actions: ED[T]['Action'][]; actions: ED[T]['Action'][];
overlap?: boolean; overlap?: boolean;
}, context: Cxt): Promise<import("../base-app-domain/UserRelation/Schema").Schema[]>; }, context: Cxt): Promise<{
userRelations: ED["userRelation"]["Schema"][];
}>;

View File

@ -2067,23 +2067,24 @@ 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, entityId, actions, overlap, filter, actionAuths, userRelations; var entity, filter, actions, overlap, actionAuthfilter, actionAuths, makeRelationIterator, urAuths, urAuthDict, urAuthGroups, userRelations;
var _this = this;
return tslib_1.__generator(this, function (_a) { return tslib_1.__generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
case 0: case 0:
entity = params.entity, entityId = params.entityId, actions = params.actions, overlap = params.overlap; entity = params.entity, filter = params.filter, actions = params.actions, overlap = params.overlap;
filter = { actionAuthfilter = {
destEntity: entity, destEntity: entity,
}; };
if (overlap) { if (overlap) {
Object.assign(filter, { Object.assign(actionAuthfilter, {
deActions: { deActions: {
$overlaps: actions, $overlaps: actions,
}, },
}); });
} }
else { else {
Object.assign(filter, { Object.assign(actionAuthfilter, {
deActions: { deActions: {
$contains: actions, $contains: actions,
}, },
@ -2092,31 +2093,124 @@ function getUserRelationsByActions(params, context) {
return [4 /*yield*/, context.select('actionAuth', { return [4 /*yield*/, context.select('actionAuth', {
data: { data: {
id: 1, id: 1,
path: 1,
relationId: 1,
relation: { relation: {
id: 1, id: 1,
userRelation$relation: { entity: 1,
},
},
filter: actionAuthfilter,
}, { 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', $entity: 'userRelation',
data: { data: {
id: 1, id: 1,
userId: 1,
relationId: 1, relationId: 1,
relation: { relation: {
id: 1, id: 1,
name: 1, name: 1,
display: 1,
}, },
entity: 1, entity: 1,
entityId: 1, entityId: 1,
}, },
filter: {
relationId: {
$in: relationIds,
}, },
}, },
}
}, },
getData: function (d) {
return d.userRelation$entity;
},
};
}
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关联
);
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, filter: filter,
}, { dontCollect: true })]; }, { dontCollect: true })];
case 1: case 1:
actionAuths = _a.sent(); rows = _c.sent();
userRelations = actionAuths.map(function (ele) { return ele.relation.userRelation$relation; }).flat(); urs = rows.map(function (ele) { return getData(ele); }).flat().filter(function (ele) { return !!ele; });
return [2 /*return*/, userRelations]; return [2 /*return*/, urs];
}
});
});
}))];
case 2:
userRelations = _a.sent();
return [2 /*return*/, {
userRelations: userRelations.flat(),
}];
} }
}); });
}); });

View File

@ -7,7 +7,7 @@ import { addFilterSegment, checkFilterContains, combineFilters } from "./filter"
import { judgeRelation } from "./relation"; import { judgeRelation } from "./relation";
import { SyncContext } from "./SyncRowStore"; import { SyncContext } from "./SyncRowStore";
import { readOnlyActions } from '../actions/action'; import { readOnlyActions } from '../actions/action';
import { difference, intersection, set, uniq, cloneDeep } from '../utils/lodash'; import { difference, intersection, set, uniq, cloneDeep, groupBy } from '../utils/lodash';
import { SYSTEM_RESERVE_ENTITIES } from "../compiler/env"; import { SYSTEM_RESERVE_ENTITIES } from "../compiler/env";
@ -2502,53 +2502,146 @@ export class RelationAuth<ED extends EntityDict & BaseEntityDict>{
*/ */
export async function getUserRelationsByActions<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>>(params: { export async function getUserRelationsByActions<ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>>(params: {
entity: T; entity: T;
entityId: string; filter: ED[T]['Selection']['filter'];
actions: ED[T]['Action'][]; actions: ED[T]['Action'][];
overlap?: boolean; overlap?: boolean;
}, context: Cxt) { }, context: Cxt) {
const { entity, entityId, actions, overlap } = params; const { entity, filter, actions, overlap } = params;
const filter = { const actionAuthfilter = {
destEntity: entity as string, destEntity: entity as string,
}; };
if (overlap) { if (overlap) {
Object.assign(filter, { Object.assign(actionAuthfilter, {
deActions: { deActions: {
$overlaps: actions, $overlaps: actions,
}, },
}); });
} }
else { else {
Object.assign(filter, { Object.assign(actionAuthfilter, {
deActions: { deActions: {
$contains: actions, $contains: actions,
}, },
}); });
} }
const actionAuths = await context.select('actionAuth', { const actionAuths = await context.select('actionAuth', {
data: { data: {
id: 1, id: 1,
path: 1,
relationId: 1,
relation: { relation: {
id: 1, id: 1,
userRelation$relation: { entity: 1,
},
},
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', $entity: 'userRelation',
data: { data: {
id: 1, id: 1,
userId: 1,
relationId: 1, relationId: 1,
relation: { relation: {
id: 1, id: 1,
name: 1, name: 1,
display: 1,
}, },
entity: 1, entity: 1,
entityId: 1, entityId: 1,
}, },
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,
},
},
getData: (d) => d[attr] && d[attr]!.map((ele: any) => getData(ele)),
}
}
};
return makeIter(entity, 0);
}
const urAuths = 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, filter,
}, { dontCollect: true }); }, { dontCollect: true });
const urs = rows.map(ele => getData(ele)).flat().filter(ele => !!ele);
const userRelations = actionAuths.map(ele => ele.relation!.userRelation$relation!).flat(); return urs as ED['userRelation']['Schema'][];
return userRelations; }
));
return {
userRelations: userRelations.flat(),
};
} }