diff --git a/src/entities/qfRound.ts b/src/entities/qfRound.ts index eb7069ae7..bfe637a14 100644 --- a/src/entities/qfRound.ts +++ b/src/entities/qfRound.ts @@ -24,9 +24,6 @@ export class QfRound extends BaseEntity { @Column('text', { nullable: true }) name: string; - @ManyToMany(type => Project, project => project.qfRounds) - projects: Project[]; - @Field({ nullable: true }) @Column({ nullable: true }) isActive: boolean; @@ -52,4 +49,7 @@ export class QfRound extends BaseEntity { @CreateDateColumn() createdAt: Date; + + @ManyToMany(type => Project, project => project.qfRounds) + projects: Project[]; } diff --git a/src/repositories/qfRoundRepository.ts b/src/repositories/qfRoundRepository.ts index b180c8678..f4474c6d5 100644 --- a/src/repositories/qfRoundRepository.ts +++ b/src/repositories/qfRoundRepository.ts @@ -109,3 +109,16 @@ export const deactivateExpiredQfRounds = async (): Promise => { [now], ); }; + +export const getRelatedProjectsOfQfRound = async ( + qfRoundId: number, +): Promise<{ slug: string; name: string }[]> => { + const query = ` + SELECT "p"."slug", "p"."title" , p.id + FROM "project" "p" + INNER JOIN "project_qf_rounds_qf_round" "qp" ON "qp"."projectId" = "p"."id" + WHERE "qp"."qfRoundId" = ${qfRoundId} + `; + + return QfRound.query(query); +}; diff --git a/src/server/adminJs/tabs/components/ProjectsInQfRound.tsx b/src/server/adminJs/tabs/components/ProjectsInQfRound.tsx new file mode 100644 index 000000000..dfec3613f --- /dev/null +++ b/src/server/adminJs/tabs/components/ProjectsInQfRound.tsx @@ -0,0 +1,35 @@ +import React, { useEffect, useState } from 'react'; +import { withTheme } from 'styled-components'; +import { Section, Label, Link } from '@adminjs/design-system'; + +const ProjectsInQfRound = props => { + const projects = props?.record?.params?.projects; + + return ( +
+ +
+ {projects.map(project => { + const { id, title, slug } = project; + const projectLink = `/admin/resources/Project/records/${id}/show`; + return ( +
+
+
+

{title}

+

{slug}

+
+ + {projectLink} + +
+
+ ); + })} +
+
+
+ ); +}; + +export default withTheme(ProjectsInQfRound); diff --git a/src/server/adminJs/tabs/qfRoundTab.ts b/src/server/adminJs/tabs/qfRoundTab.ts index 492c8a6cb..4422c14f5 100644 --- a/src/server/adminJs/tabs/qfRoundTab.ts +++ b/src/server/adminJs/tabs/qfRoundTab.ts @@ -12,9 +12,13 @@ import { AdminJsContextInterface, AdminJsRequestInterface, } from '../adminJs-types'; -import { ValidationError } from 'adminjs'; +import adminJs, { ValidationError } from 'adminjs'; import { isQfRoundHasEnded } from '../../../services/qfRoundService'; -import { findQfRoundById } from '../../../repositories/qfRoundRepository'; +import { + findQfRoundById, + getRelatedProjectsOfQfRound, +} from '../../../repositories/qfRoundRepository'; +import { RecordJSON } from 'adminjs/src/frontend/interfaces/record-json.interface'; export const refreshMaterializedViews = async ( response, @@ -24,6 +28,26 @@ export const refreshMaterializedViews = async ( return response; }; +export const fillProjects: After = async ( + response, + _request, + _context, +) => { + const record: RecordJSON = response.record || {}; + const qfRoundId = record.params.qfRoundId || record.params.id; + const projects = await getRelatedProjectsOfQfRound(qfRoundId); + + const adminJsBaseUrl = process.env.SERVER_URL; + response.record = { + ...record, + params: { + ...record.params, + projects, + }, + }; + return response; +}; + export const qfRoundTab = { resource: QfRound, options: { @@ -47,11 +71,15 @@ export const qfRoundTab = { isVisible: true, }, projects: { + type: 'mixed', isVisible: { - list: true, - edit: false, + list: false, filter: false, show: true, + edit: false, + }, + components: { + show: adminJs.bundle('./components/ProjectsInQfRound'), }, }, createdAt: { @@ -88,6 +116,11 @@ export const qfRoundTab = { canAccessQfRoundAction({ currentAdmin }, ResourceActions.NEW), after: refreshMaterializedViews, }, + show: { + isAccessible: ({ currentAdmin }) => + canAccessQfRoundAction({ currentAdmin }, ResourceActions.SHOW), + after: fillProjects, + }, edit: { isAccessible: ({ currentAdmin }) => canAccessQfRoundAction({ currentAdmin }, ResourceActions.EDIT),