Skip to content

Commit

Permalink
add isImported And categories to project tab
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosQ96 committed Sep 12, 2024
1 parent 52d5afa commit 8d30b1e
Showing 1 changed file with 77 additions and 2 deletions.
79 changes: 77 additions & 2 deletions src/server/adminJs/tabs/projectsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { User } from '../../../entities/user';
import { refreshProjectEstimatedMatchingView } from '../../../services/projectViewsService';
import { extractAdminJsReferrerUrlParams } from '../adminJs';
import { relateManyProjectsToQfRound } from '../../../repositories/qfRoundRepository2';
import { Category } from '../../../entities/category';

// add queries depending on which filters were selected
export const buildProjectsQuery = (
Expand Down Expand Up @@ -486,6 +487,15 @@ export const fillSocialProfileAndQfRounds: After<
const projectUpdates = await findProjectUpdatesByProjectId(projectId);
const project = await findProjectById(projectId);
const adminJsBaseUrl = process.env.SERVER_URL;
let categories;
if (project) {
const categoryIds = project!.categories.map(cat => cat.id);
categories = await Category

Check failure on line 493 in src/server/adminJs/tabs/projectsTab.ts

View workflow job for this annotation

GitHub Actions / test

Delete `⏎············`
.createQueryBuilder('category')
.where('category.id IN (:...ids)', { ids: categoryIds })

Check failure on line 495 in src/server/adminJs/tabs/projectsTab.ts

View workflow job for this annotation

GitHub Actions / test

Replace `············` with `······`
.orderBy('category.name', 'ASC')

Check failure on line 496 in src/server/adminJs/tabs/projectsTab.ts

View workflow job for this annotation

GitHub Actions / test

Delete `······`
.getMany();

Check failure on line 497 in src/server/adminJs/tabs/projectsTab.ts

View workflow job for this annotation

GitHub Actions / test

Delete `······`
}
response.record = {
...record,
params: {
Expand All @@ -499,6 +509,10 @@ export const fillSocialProfileAndQfRounds: After<
adminJsBaseUrl,
},
};

if (categories) {
response.record.params.categories = categories.map(cat => `${cat.id} - ${cat.name}`);

Check failure on line 514 in src/server/adminJs/tabs/projectsTab.ts

View workflow job for this annotation

GitHub Actions / test

Replace `cat·=>·`${cat.id}·-·${cat.name}`` with `⏎······cat·=>·`${cat.id}·-·${cat.name}`,⏎····`
}
return response;
};

Expand Down Expand Up @@ -660,7 +674,7 @@ export const projectsTab = {
id: {
isVisible: {
list: false,
filter: false,
filter: true,
show: true,
edit: false,
},
Expand Down Expand Up @@ -831,12 +845,34 @@ export const projectsTab = {
edit: false,
},
},
categoryIds: {
type: 'reference',
isArray: true,
reference: 'Category',
isVisible: {
list: false,
filter: false,
show: true,
edit: true,
},
availableValues: async (_record) => {

Check failure on line 858 in src/server/adminJs/tabs/projectsTab.ts

View workflow job for this annotation

GitHub Actions / test

Replace `(_record)` with `_record`
const categories = await Category

Check failure on line 859 in src/server/adminJs/tabs/projectsTab.ts

View workflow job for this annotation

GitHub Actions / test

Delete `⏎············`
.createQueryBuilder('category')
.where('category.isActive = :isActive', { isActive: true })
.orderBy('category.name', 'ASC')
.getMany();

Check failure on line 863 in src/server/adminJs/tabs/projectsTab.ts

View workflow job for this annotation

GitHub Actions / test

Delete `··········`
return categories.map(category => ({
value: category.id,
label: `${category.id} - ${category.name}`,
}));
},
},
isImported: {
isVisible: {
list: false,
filter: true,
show: true,
edit: false,
edit: true,
},
},
totalReactions: {
Expand Down Expand Up @@ -924,6 +960,19 @@ export const projectsTab = {
isVisible: false,
isAccessible: ({ currentAdmin }) =>
canAccessProjectAction({ currentAdmin }, ResourceActions.NEW),
before: async (request) => {

Check failure on line 963 in src/server/adminJs/tabs/projectsTab.ts

View workflow job for this annotation

GitHub Actions / test

Replace `(request)` with `request`
if (request.payload.categories) {
request.payload.categories = (request.payload.categories as string[]).map(id => ({ id: parseInt(id, 10) }));

Check failure on line 965 in src/server/adminJs/tabs/projectsTab.ts

View workflow job for this annotation

GitHub Actions / test

Replace `request.payload.categories·as·string[]` with `⏎··············request.payload.categories·as·string[]⏎············`
}
return request;
},
after: async (response) => {
const { record, request } = response;
if (request.payload.categoryIds) {
await saveCategories(record.params.id, request.payload.categoryIds);
}
return response;
},
},
bulkDelete: {
isVisible: false,
Expand Down Expand Up @@ -1014,6 +1063,9 @@ export const projectsTab = {
// We put these status changes in payload, so in after hook we would know to send notification for users
request.payload.statusChanges = statusChanges.join(',');
}
if (request.payload.categories) {
request.payload.categories = (request.payload.categories as string[]).map(id => ({ id: parseInt(id, 10) }));
}
return request;
},
after: async (
Expand Down Expand Up @@ -1155,6 +1207,7 @@ export const projectsTab = {
refreshUserProjectPowerView(),
refreshProjectFuturePowerView(),
refreshProjectPowerView(),
saveCategories(project!.id, request?.payload?.categoryIds || [])
]);
return request;
},
Expand Down Expand Up @@ -1350,3 +1403,25 @@ export const projectsTab = {
},
},
};

async function saveCategories(projectId: number, categoryIds: string[]) {
if (categoryIds?.length === 0) return;

const project = await Project
.createQueryBuilder('project')
.leftJoinAndSelect('project.categories', 'category')
.where('project.id = :id', { id: projectId })
.getOne();

if (!project) {
throw new Error('Project not found');
}

const categories = await Category
.createQueryBuilder('category')
.where('category.id IN (:...ids)', { ids: categoryIds })
.getMany();

project.categories = categories;
await project.save();
}

0 comments on commit 8d30b1e

Please sign in to comment.