我已经建立了一个基于谷歌的“人员查看器”模板的评论应用程序,允许经理为他们的直接报告创建和编辑评论。
这些评论应该被经理链的经理们读懂。为此,我创建了一个服务器脚本,假设EmployeeEmail将额外存储在评审中。但也许还有更好的选择?
function getDirectReportsChainForUser_(query) {
var userQuery = app.models.Directory.newQuery();
userQuery.filters.PrimaryEmail._equals = query.parameters.PrimaryEmail;
userQuery.prefetch.DirectReports._add();
userQuery.prefetch.DirectReports.DirectReports._add();
var users = userQuery.run();
if (users.length === 0) {
return [];
}
var user = users[0];
var directs = user.DirectReports;
var records = [];
for (var i = 0; i <= directs.length; i++) {
records.push(directs[i].PrimaryEmail);
}
// The following lines are based on the asumption that the EmployeeEmail
// will be stored in the review in case that there is no better alternative.
//The question that then remains is how to recursively add the DirectReports
//of the DirectReports to the array???
var reviewQuery = app.models.Reviews.newQuery();
reviewQuery.filters.EmployeeEmail._in = records;
return reviewQuery.run();
}经理应该能够确定他的一个或多个副手是否也能读到他所在单位的评论。我的想法是通过目录和评审模型之间的多对多关系来解决这个问题,但我不知道如何实现它?
此外,一旦经理或其副手离职,行政当局就有可能解除这种联系,并将审查与继任人重新联系起来。因此,我正在考虑在管理页面中集成一个multiselect。这是否可行呢?
发布于 2018-03-29 15:58:19
在这里,我至少看到两个截然不同的问题:
是否有更好的方法将目录模型的记录与普通数据模型关联起来,而不只是在数据模型中添加主电子邮件字段
不,此时不可能在数据(SQL/Drive表)和目录模型之间建立关系。
如何递归地为用户获取所有直接报告
应用程序制造商的目录模型( Directory )是G套装Admin的目录API上的一个包装器,它只公开了一小部分功能强大的功能。当您在相应的应用程序脚本中添加目录模型应用程序制造者自动插件时,预先服务:

由于我们已经配置了Directory,所以我们可以释放它的全部功能,并通过一个调用轻松地获取所有经理的下属(如果您需要支持分页,则是多个调用)。为了做到这一点,我们将使用带有Users.List查询参数的managerId API方法(唯一允许我们查询树下所有下属的方法)。以下是引用完整搜索文档中引用的最小搜索查询参数集(如果没有这些参数查询将无法工作或无法以我们需要的方式工作):
customer查询参数。必须为customer或domain参数提供。admin_view是默认值,因此我们需要用domain_view覆盖它)。/**
* Fetches all reviews associated with all subordinate employees (both direct
* and indirect reports).
*/
function getAllReportsEmails(managerId) {
var emails = [];
var result = AdminDirectory.Users.list({
domain: 'ENTER HERE YOUR DOMAIN (exapmle.com)',
query: 'managerId=' + managerId,
viewType: 'domain_public',
maxResults: 100
});
if (result.users) {
emails = result.users.map(function (user) {
return user.primaryEmail;
});
}
return emails;
}
/**
* Fetches all reviews associated with all subordinate employees (both direct
* and indirect reports).
*/
function getAllReportsReviewsForManager_(query) {
var userQuery = app.models.Directory.newQuery();
// For better security I would recommend to use
// Session.getActiveUser().getEmail() instead of parameter
// passed from the client.
userQuery.filters.PrimaryEmail._equals = Session.getActiveUser().getEmail();
var users = userQuery.run();
if (users.length === 0) {
return [];
}
var manager = users[0];
var managerId = manager._key;
var allReportsEmails = getAllReportsEmails(managerId);
var reviewQuery = app.models.Reviews.newQuery();
reviewQuery.filters.EmployeeEmail._in = allReportsEmails;
return reviewQuery.run();
}发布于 2018-03-29 20:28:38
Pavel,我试图将您给我的想法集成到一个服务器脚本中,该脚本返回经理及其整个下属链(直接报告+间接报告)的数组,这样我就可以在需要时使用它。我变成了一个递归函数,以获得下一个较低级别上的直接报告和间接报告。有办法弄到整个链条吗?
function getSubordinatesChainForUser(query) {
var userQuery = app.models.Directory.newQuery();
userQuery.filters.PrimaryEmail._equals = Session.getActiveUser().getEmail();
userQuery.prefetch.DirectReports._add();
userQuery.prefetch.DirectReports.DirectReports._add();
var users = userQuery.run();
if (users.length === 0) {
return [];
}
var userEmails = users.map(function(manager){
var employeeEmails = manager.DirectReports.map(function(employee){
return employee.PrimaryEmail;
});
return manager.PrimaryEmail + ',' + employeeEmails;
});
return userEmails;
}https://stackoverflow.com/questions/49506812
复制相似问题