@@ -238,45 +238,84 @@ private async Task<Results<Ok<Response>, NotFound>> ListObserverGuidesAsObserver
238238 G."WebsiteUrl",
239239 G."FilePath",
240240 G."UploadedFileName",
241- COALESCE(G."LastModifiedOn", G."CreatedOn") AS"CreatedOn",
242- n."Name" AS "CreatedBy",
243- EXISTS (
241+ G."CreatedOn",
242+ G."CreatedBy"
243+ FROM
244+ (
244245 SELECT
245- 1
246+ G."Id",
247+ G."Title",
248+ G."FileName",
249+ G."MimeType",
250+ G."GuideType",
251+ G."Text",
252+ G."WebsiteUrl",
253+ G."FilePath",
254+ G."UploadedFileName",
255+ COALESCE(G."LastModifiedOn", G."CreatedOn") AS"CreatedOn",
256+ n."Name" AS "CreatedBy"
246257 FROM
247- "GetMonitoringNgoDetails" (@electionRoundId, @ngoId)
258+ "CoalitionGuideAccess" CGA
259+ INNER JOIN "Coalitions" C ON CGA."CoalitionId" = C."Id"
260+ INNER JOIN "ObserversGuides" G ON CGA."GuideId" = G."Id"
261+ INNER JOIN "MonitoringNgos" mn on g."MonitoringNgoId" = mn."Id"
262+ INNER JOIN "Ngos" n on n."Id" = mn."NgoId"
248263 WHERE
249- "MonitoringNgoId" = G."MonitoringNgoId"
250- ) AS "IsGuideOwner"
251- FROM
252- "CoalitionGuideAccess" CGA
253- INNER JOIN "Coalitions" C ON CGA."CoalitionId" = C."Id"
254- INNER JOIN "ObserversGuides" G ON CGA."GuideId" = G."Id"
255- INNER JOIN "MonitoringNgos" mn on g."MonitoringNgoId" = mn."Id"
256- INNER JOIN "Ngos" n on n."Id" = mn."NgoId"
257- WHERE
258- CGA."MonitoringNgoId" = (
264+ CGA."MonitoringNgoId" = (
265+ SELECT
266+ "MonitoringNgoId"
267+ FROM
268+ "GetMonitoringNgoDetails" (@electionRoundId, @ngoId)
269+ )
270+ AND g."IsDeleted" = false
271+ AND C."ElectionRoundId" = @electionRoundId
272+ AND (
273+ (
274+ SELECT
275+ "CoalitionId" IS NOT NULL
276+ FROM
277+ "GetMonitoringNgoDetails" (@electionRoundId, @ngoId)
278+ )
279+ OR (
280+ SELECT
281+ "IsCoalitionLeader"
282+ FROM
283+ "GetMonitoringNgoDetails" (@electionRoundId, @ngoId)
284+ )
285+ )
286+ UNION
259287 SELECT
260- "MonitoringNgoId"
288+ G."Id",
289+ G."Title",
290+ G."FileName",
291+ G."MimeType",
292+ G."GuideType",
293+ G."Text",
294+ G."WebsiteUrl",
295+ G."FilePath",
296+ G."UploadedFileName",
297+ COALESCE(G."LastModifiedOn", G."CreatedOn") AS"CreatedOn",
298+ n."Name" AS "CreatedBy"
261299 FROM
262- "GetMonitoringNgoDetails" (@electionRoundId, @ngoId)
263- )
264- AND C."ElectionRoundId" = @electionRoundId
265- AND g."IsDeleted" = false
266- AND (
267- (
268- SELECT
269- "CoalitionId" IS NOT NULL
270- FROM
271- "GetMonitoringNgoDetails" (@electionRoundId, @ngoId)
272- )
273- OR (
274- SELECT
275- "IsCoalitionLeader"
276- FROM
277- "GetMonitoringNgoDetails" (@electionRoundId, @ngoId)
278- )
279- )
300+ "ObserversGuides" G
301+ INNER JOIN "MonitoringNgos" MN ON G."MonitoringNgoId" = MN."Id"
302+ INNER JOIN "Ngos" n on n."Id" = mn."NgoId"
303+ WHERE
304+ MN."ElectionRoundId" = @electionRoundId
305+ AND g."IsDeleted" = false
306+ AND G."MonitoringNgoId" = (
307+ SELECT
308+ "MonitoringNgoId"
309+ FROM
310+ "GetMonitoringNgoDetails" (@electionRoundId, @ngoId)
311+ )
312+ AND (
313+ SELECT
314+ "CoalitionId" IS NULL
315+ FROM
316+ "GetMonitoringNgoDetails" (@electionRoundId, @ngoId)
317+ )
318+ ) G
280319 """ ;
281320
282321 var queryArgs = new { electionRoundId , ngoId = ngo . NgoId } ;
0 commit comments