From 5d5a0553aee038bd97d24fa048e8acd599fac8ed Mon Sep 17 00:00:00 2001 From: teske00 Date: Thu, 15 Aug 2024 13:26:13 +0200 Subject: [PATCH] Dynamically filter qa type list based on protocol_major --- .../controllers/governance-action-type.js | 59 ++++++++++++++++++- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/backend/src/api/governance-action-type/controllers/governance-action-type.js b/backend/src/api/governance-action-type/controllers/governance-action-type.js index fc2ad1e..a1a5ee0 100644 --- a/backend/src/api/governance-action-type/controllers/governance-action-type.js +++ b/backend/src/api/governance-action-type/controllers/governance-action-type.js @@ -1,9 +1,62 @@ -'use strict'; +// @ts-nocheck +"use strict"; /** * governance-action-type controller */ -const { createCoreController } = require('@strapi/strapi').factories; +const axios = require("axios"); +const { createCoreController } = require("@strapi/strapi").factories; -module.exports = createCoreController('api::governance-action-type.governance-action-type'); +module.exports = createCoreController( + "api::governance-action-type.governance-action-type", + ({ strapi }) => ({ + async find(ctx) { + const sanitizedQueryParams = await this.sanitizeQuery(ctx); + + try { + const { data } = await axios.get( + `${process.env.GOVTOOL_API_BASE_URL}/epoch/params` + ); + + if (data) { + if (!sanitizedQueryParams.filters) { + sanitizedQueryParams.filters = {}; + } + + if (+data?.protocol_major < 9) { + sanitizedQueryParams.filters = { + ...sanitizedQueryParams.filters, + $and: [ + { + gov_action_type_name: { + $ne: "Treasury", + }, + }, + { + gov_action_type_name: { + $ne: "Info", + }, + }, + ], + }; + } + if (+data?.protocol_major === 9) { + sanitizedQueryParams.filters = { + ...sanitizedQueryParams.filters, + gov_action_type_name: { + $ne: "Treasury", + }, + }; + } + } + } catch (error) {} + + const { results, pagination } = await strapi + .service("api::governance-action-type.governance-action-type") + .find(sanitizedQueryParams); + + return this.transformResponse(results, { pagination }); + }, + }) +);