@@ -8,12 +8,15 @@ import { CreateControlDto } from './dto/create-control.dto';
88
99const controlInclude = {
1010 policies : {
11+ where : { archivedAt : null } ,
1112 select : { status : true , id : true , name : true } ,
1213 } ,
1314 tasks : {
15+ where : { archivedAt : null } ,
1416 select : { id : true , title : true , status : true } ,
1517 } ,
1618 requirementsMapped : {
19+ where : { archivedAt : null } ,
1720 include : {
1821 frameworkInstance : {
1922 include : { framework : true , customFramework : true } ,
@@ -42,6 +45,7 @@ export class ControlsService {
4245 ) {
4346 const where : Prisma . ControlWhereInput = {
4447 organizationId,
48+ archivedAt : null ,
4549 ...( options . name && {
4650 name : { contains : options . name , mode : Prisma . QueryMode . insensitive } ,
4751 } ) ,
@@ -72,10 +76,11 @@ export class ControlsService {
7276 const control = await db . control . findUnique ( {
7377 where : { id : controlId , organizationId } ,
7478 include : {
75- policies : true ,
76- tasks : true ,
79+ policies : { where : { archivedAt : null } } ,
80+ tasks : { where : { archivedAt : null } } ,
7781 controlDocumentTypes : true ,
7882 requirementsMapped : {
83+ where : { archivedAt : null } ,
7984 include : {
8085 frameworkInstance : {
8186 include : { framework : true , customFramework : true } ,
@@ -145,12 +150,12 @@ export class ControlsService {
145150 async getOptions ( organizationId : string ) {
146151 const [ policies , tasks , frameworkInstances ] = await Promise . all ( [
147152 db . policy . findMany ( {
148- where : { organizationId } ,
153+ where : { organizationId, isArchived : false , archivedAt : null } ,
149154 select : { id : true , name : true } ,
150155 orderBy : { name : 'asc' } ,
151156 } ) ,
152157 db . task . findMany ( {
153- where : { organizationId } ,
158+ where : { organizationId, archivedAt : null } ,
154159 select : { id : true , title : true } ,
155160 orderBy : { title : 'asc' } ,
156161 } ) ,
@@ -300,8 +305,11 @@ export class ControlsService {
300305 ) : Promise < string [ ] > {
301306 if ( ! policyIds || policyIds . length === 0 ) return [ ] ;
302307 const uniqueIds = Array . from ( new Set ( policyIds ) ) ;
308+ // Exclude both user-archived (isArchived) and sync-archived (archivedAt)
309+ // policies. Checking only archivedAt would let user-archived policies
310+ // get re-linked to a control and surface back through the UI.
303311 const policies = await db . policy . findMany ( {
304- where : { id : { in : uniqueIds } , organizationId } ,
312+ where : { id : { in : uniqueIds } , organizationId, archivedAt : null , isArchived : false } ,
305313 select : { id : true } ,
306314 } ) ;
307315 if ( policies . length !== uniqueIds . length ) {
@@ -317,7 +325,7 @@ export class ControlsService {
317325 if ( ! taskIds || taskIds . length === 0 ) return [ ] ;
318326 const uniqueIds = Array . from ( new Set ( taskIds ) ) ;
319327 const tasks = await db . task . findMany ( {
320- where : { id : { in : uniqueIds } , organizationId } ,
328+ where : { id : { in : uniqueIds } , organizationId, archivedAt : null } ,
321329 select : { id : true } ,
322330 } ) ;
323331 if ( tasks . length !== uniqueIds . length ) {
@@ -426,7 +434,7 @@ export class ControlsService {
426434 await this . ensureControl ( controlId , organizationId ) ;
427435
428436 const policies = await db . policy . findMany ( {
429- where : { id : { in : policyIds } , organizationId } ,
437+ where : { id : { in : policyIds } , organizationId, archivedAt : null } ,
430438 select : { id : true } ,
431439 } ) ;
432440 if ( policies . length === 0 ) {
@@ -449,7 +457,7 @@ export class ControlsService {
449457 await this . ensureControl ( controlId , organizationId ) ;
450458
451459 const tasks = await db . task . findMany ( {
452- where : { id : { in : taskIds } , organizationId } ,
460+ where : { id : { in : taskIds } , organizationId, archivedAt : null } ,
453461 select : { id : true } ,
454462 } ) ;
455463 if ( tasks . length === 0 ) {
0 commit comments