Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MARXAN-1510-owner-emails-in-admin-published-projects-panel #1053

Merged
merged 4 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export class PublishedProject {
@Column('uuid', { name: 'export_id' })
exportId?: string;

ownerEmails?: string[];

@OneToOne(() => Project)
@JoinColumn({ name: 'id' })
originalProject?: Project;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import {
UpdatePublishedProjectDto,
} from '@marxan-api/modules/published-project/dto/create-published-project.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, SelectQueryBuilder } from 'typeorm';
import { In, Repository, SelectQueryBuilder } from 'typeorm';
import { AppConfig } from '@marxan-api/utils/config.utils';
import { publishedProjectResource } from '@marxan-api/modules/published-project/published-project.resource';
import { FetchSpecification } from 'nestjs-base-service';
import { UsersService } from '../users/users.service';
import { ExportId } from '../clone';
import { ExportRepository } from '../clone/export/application/export-repository.port';
import { Project } from '../projects/project.api.entity';
import { UsersProjectsApiEntity } from '../access-control/projects-acl/entity/users-projects.api.entity';
import { ProjectRoles } from '../access-control/projects-acl/dto/user-role-project.dto';
import { groupBy } from 'lodash';

@Injectable()
export class PublishedProjectCrudService extends AppBaseService<
Expand All @@ -28,6 +32,10 @@ export class PublishedProjectCrudService extends AppBaseService<
constructor(
@InjectRepository(PublishedProject)
protected repository: Repository<PublishedProject>,
@InjectRepository(UsersProjectsApiEntity)
private usersProjectsRepo: Repository<UsersProjectsApiEntity>,
@InjectRepository(Project)
private projectRepository: Repository<Project>,
private readonly usersService: UsersService,
private exportRepo: ExportRepository,
) {
Expand All @@ -54,6 +62,7 @@ export class PublishedProjectCrudService extends AppBaseService<
'originalProject',
'pngData',
'exportId',
'ownerEmails',
],
keyForAttribute: 'camelCase',
originalProject: {
Expand Down Expand Up @@ -125,4 +134,39 @@ export class PublishedProjectCrudService extends AppBaseService<

return query;
}

async extendFindAllResults(
entitiesAndCount: [PublishedProject[], number],
fetchSpecification?: FetchSpecification,
info?: ProjectsRequest,
): Promise<[PublishedProject[], number]> {
const userId = info?.authenticatedUser?.id;

if (!userId || !(await this.usersService.isPlatformAdmin(userId))) {
return entitiesAndCount;
}

const allOwnersOfAllProjectsPlusEmail = await this.usersProjectsRepo.query(
`
select up.project_id, u.email
from users_projects as up
left join users as u
on (u."id" = up.user_id)
where up.project_id IN (SELECT id FROM published_projects) AND up.role_id = $1;`,
[ProjectRoles.project_owner],
);
const ownersPerProject = groupBy(
allOwnersOfAllProjectsPlusEmail,
(item) => item.project_id,
);

const extendedEntities: PublishedProject[] = entitiesAndCount[0].map(
(entity) => ({
...entity,
ownerEmails: ownersPerProject[entity.id].map((item) => item.email),
}),
);

return [extendedEntities, entitiesAndCount[1]];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { ProjectsService } from '../projects/projects.service';
import { Scenario } from '../scenarios/scenario.api.entity';
import { getScenarioSnapshot } from '@marxan-api/utils/webshot.utils';
import { ExportRepository } from '../clone/export/application/export-repository.port';
import { assertDefined } from '@marxan/utils';
import { ExportId } from '@marxan-api/modules/clone';

export const notFound = Symbol(`project not found`);
Expand Down