11import {
22 CreateSponsorTask ,
3+ isAdmin ,
34 isHead ,
45 ReimbursementRequestData ,
56 SpendingBarData ,
@@ -15,6 +16,7 @@ import {
1516 getSponsorTierQueryArgs
1617} from '../prisma-query-args/sponsor.query.args' ;
1718import {
19+ AccessDeniedAdminOnlyException ,
1820 AccessDeniedException ,
1921 DeletedException ,
2022 HttpException ,
@@ -176,17 +178,25 @@ export default class FinanceServices {
176178 * @param name tier name
177179 * @param organization current organization of the current user
178180 * @param colorHexCode tier color
181+ * @param minSupportValue minimum support value for the tier
179182 * @returns newly created sponsor tier
180183 */
181- static async createSponsorTier ( submitter : User , name : string , organization : Organization , colorHexCode : string ) {
184+ static async createSponsorTier (
185+ submitter : User ,
186+ name : string ,
187+ organization : Organization ,
188+ colorHexCode : string ,
189+ minSupportValue : number
190+ ) : Promise < SponsorTier > {
182191 if ( ! ( await userHasPermission ( submitter . userId , organization . organizationId , isHead ) ) )
183192 throw new AccessDeniedException ( 'Only heads can create a sponsor tier' ) ;
184193
185194 const sponsorTier = await prisma . sponsor_Tier . create ( {
186195 data : {
187196 name,
188197 organizationId : organization . organizationId ,
189- colorHexCode
198+ colorHexCode,
199+ minSupportValue
190200 } ,
191201 include : {
192202 organization : true
@@ -587,10 +597,97 @@ export default class FinanceServices {
587597 */
588598 static async getAllSponsorTiers ( organization : Organization ) : Promise < SponsorTier [ ] > {
589599 const allSponsorTiers = await prisma . sponsor_Tier . findMany ( {
590- where : { organizationId : organization . organizationId } ,
600+ where : { organizationId : organization . organizationId , dateDeleted : null } ,
601+ orderBy : { minSupportValue : 'asc' } ,
591602 ...getSponsorTierQueryArgs ( organization . organizationId )
592603 } ) ;
593604
594605 return allSponsorTiers ;
595606 }
607+
608+ /**
609+ * Soft deletes a given sponsor tier
610+ * @param sponsorTierId the id of the sponsor tier that is getting deleted
611+ * @param deleter the person deleting the sponsor tier
612+ * @param organization the organization the person deleting belongs to
613+ * @returns the deleted sponsor tier
614+ */
615+ static async deleteSponsorTier ( sponsorTierId : string , deleter : User , organization : Organization ) : Promise < SponsorTier > {
616+ const sponsorTier = await prisma . sponsor_Tier . findUnique ( {
617+ where : { sponsorTierId }
618+ } ) ;
619+
620+ if ( ! ( await userHasPermission ( deleter . userId , organization . organizationId , isAdmin ) ) ) {
621+ throw new AccessDeniedAdminOnlyException ( 'delete a sponsor tier' ) ;
622+ }
623+
624+ if ( ! sponsorTier ) throw new NotFoundException ( 'Sponsor Tier' , sponsorTierId ) ;
625+ if ( sponsorTier . organizationId !== organization . organizationId ) throw new InvalidOrganizationException ( 'Sponsor Tier' ) ;
626+ if ( sponsorTier . dateDeleted ) throw new DeletedException ( 'Sponsor Tier' , sponsorTierId ) ;
627+
628+ const associatedSponsors = await prisma . sponsor . count ( {
629+ where : { sponsorTierId : sponsorTier . sponsorTierId , dateDeleted : null }
630+ } ) ;
631+
632+ if ( associatedSponsors > 0 ) {
633+ throw new HttpException (
634+ 400 ,
635+ `Cannot delete Sponsor Tier "${ sponsorTier . name } " because it is associated with existing sponsors.`
636+ ) ;
637+ }
638+
639+ const deletedSponsorTier = await prisma . sponsor_Tier . update ( {
640+ where : { sponsorTierId } ,
641+ data : { dateDeleted : new Date ( ) , deleter : { connect : { userId : deleter . userId } } } ,
642+ ...getSponsorTierQueryArgs ( organization . organizationId )
643+ } ) ;
644+
645+ return deletedSponsorTier ;
646+ }
647+
648+ /**
649+ * Edits a sponsor tier.
650+ * @param submitter current user editing the sponsor tier
651+ * @param organization current organization of the current user
652+ * @param sponsorTierId id of the sponsor tier to be edited
653+ * @param name updated tier name
654+ * @param colorHexCode updated tier color
655+ * @param minSupportValue updated minimum support value for the tier
656+ * @returns the updated sponsor tier
657+ * @throws AccessDeniedAdminOnlyException if the user lacks permissions.
658+ * @throws NotFoundException if the sponsor tier is not found.
659+ */
660+ static async editSponsorTier (
661+ submitter : User ,
662+ organization : Organization ,
663+ sponsorTierId : string ,
664+ name : string ,
665+ colorHexCode : string ,
666+ minSupportValue : number
667+ ) : Promise < SponsorTier > {
668+ if ( ! ( await userHasPermission ( submitter . userId , organization . organizationId , isAdmin ) ) )
669+ throw new AccessDeniedAdminOnlyException ( 'edit a sponsor tier' ) ;
670+
671+ const oldSponsorTier = await prisma . sponsor_Tier . findUnique ( {
672+ where : {
673+ sponsorTierId,
674+ organizationId : organization . organizationId
675+ }
676+ } ) ;
677+
678+ if ( ! oldSponsorTier ) throw new NotFoundException ( 'Sponsor Tier' , sponsorTierId ) ;
679+ if ( oldSponsorTier . dateDeleted ) throw new DeletedException ( 'Sponsor Tier' , sponsorTierId ) ;
680+
681+ const updatedSponsorTier = await prisma . sponsor_Tier . update ( {
682+ where : { sponsorTierId : oldSponsorTier . sponsorTierId } ,
683+ data : {
684+ name,
685+ colorHexCode,
686+ minSupportValue
687+ } ,
688+ ...getSponsorTierQueryArgs ( organization . organizationId )
689+ } ) ;
690+
691+ return updatedSponsorTier ;
692+ }
596693}
0 commit comments