diff --git a/__tests__/archive.ts b/__tests__/archive.ts index 6be5079c13..79c09b93a0 100644 --- a/__tests__/archive.ts +++ b/__tests__/archive.ts @@ -692,6 +692,27 @@ describe('archive queries', () => { }); }); + it('should exclude items whose post has been deleted', async () => { + await con.getRepository(Post).update({ id: 'post-6' }, { deleted: true }); + + const res = await client.query(archiveQuery, { + variables: { + subjectType: ArchiveSubjectType.Post, + rankingType: ArchiveRankingType.Best, + scopeType: ArchiveScopeType.Tag, + scopeId: 'webdev', + periodType: ArchivePeriodType.Month, + year: 2026, + month: 3, + }, + }); + + expect(res.errors).toBeFalsy(); + expect(res.data.archive).toMatchObject({ + items: [], + }); + }); + it('should return only published archives from archiveIndex', async () => { const res = await client.query(archiveIndexQuery, { variables: { diff --git a/src/graphorm/index.ts b/src/graphorm/index.ts index 5f40d42b9e..ffd6120a02 100644 --- a/src/graphorm/index.ts +++ b/src/graphorm/index.ts @@ -2652,6 +2652,9 @@ const obj = new GraphORM({ customRelation: (_, parentAlias, childAlias, qb): QueryBuilder => qb .where(`${childAlias}."archiveId" = "${parentAlias}".id`) + .andWhere( + `EXISTS (SELECT 1 FROM "post" "p" WHERE "p".id = ${childAlias}."subjectId" AND "p"."deleted" = false)`, + ) .orderBy(`${childAlias}.rank`, 'ASC'), }, }, @@ -2689,7 +2692,9 @@ const obj = new GraphORM({ relation: { isMany: false, customRelation: (_, parentAlias, childAlias, qb): QueryBuilder => - qb.where(`${childAlias}.id = "${parentAlias}"."subjectId"`), + qb + .where(`${childAlias}.id = "${parentAlias}"."subjectId"`) + .andWhere(`${childAlias}."deleted" = false`), }, }, },