All files / src/features/white-labeling white-labeling.service.ts

100% Statements 18/18
100% Branches 6/6
100% Functions 7/7
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99                12x     4x 4x 4x 4x       3x 3x     2x     2x     2x           3x             1x 1x                           1x 1x                           1x                                           6x          
import { Injectable } from '@nestjs/common'
import { PrismaService } from '../../database/prisma.service'
import { ConfigScope, Configuration, Prisma } from '@prisma/client'
import { EventConfig, DeepPartial } from '@fundraising/white-labeling'
import { WhiteLabelingMapper } from './white-labeling.mapper'
 
@Injectable()
export class WhiteLabelingService {
    constructor(private prisma: PrismaService) {}
 
    async getGlobalSettings(): Promise<EventConfig> {
        const config = await this.getConfig(ConfigScope.GLOBAL)
        const resolved = WhiteLabelingMapper.toEventConfig(config || ({} as Configuration))
        resolved.id = 'GLOBAL'
        return resolved
    }
 
    async getEventSettings(slug: string): Promise<(EventConfig & { isOverride: boolean }) | null> {
        const event = await this.prisma.event.findUnique({ where: { slug } })
        if (!event) return null
 
        // 1. Fetch Global Config
        const globalSettings = await this.getGlobalSettings()
 
        // 2. Fetch Event Config
        const eventConfigDb = await this.getConfig(ConfigScope.EVENT, event.id)
 
        // 3. Merge (Event > Global > Default)
        const mapped = WhiteLabelingMapper.toEventConfig(
            eventConfigDb || ({} as Configuration),
            event,
            globalSettings,
        )
 
        return {
            ...mapped,
            isOverride: !!eventConfigDb,
        }
    }
 
    async updateGlobalSettings(data: DeepPartial<EventConfig>) {
        const payload = WhiteLabelingMapper.toDbPayload(data)
        return this.prisma.configuration.upsert({
            where: {
                scope_entityId: { scope: ConfigScope.GLOBAL, entityId: 'GLOBAL' },
            },
            update: payload,
            create: {
                ...(payload as any),
                scope: ConfigScope.GLOBAL,
                entityId: 'GLOBAL',
            },
        })
    }
 
    async updateEventSettings(eventId: string, data: DeepPartial<EventConfig>) {
        const payload = WhiteLabelingMapper.toDbPayload(data)
        return this.prisma.configuration.upsert({
            where: {
                scope_entityId: { scope: ConfigScope.EVENT, entityId: eventId },
            },
            update: payload,
            create: {
                ...(payload as any),
                scope: ConfigScope.EVENT,
                entityId: eventId,
            },
        })
    }
 
    async resetEventSettings(eventId: string) {
        return this.prisma.configuration.updateMany({
            where: { scope: ConfigScope.EVENT, entityId: eventId },
            data: {
                organization: null,
                address: null,
                phone: null,
                logo: null,
                email: null,
                website: null,
                themeVariables: Prisma.DbNull,
                assets: Prisma.DbNull,
                event: Prisma.DbNull,
                communication: Prisma.DbNull,
                form: Prisma.DbNull,
                payment: Prisma.DbNull,
                socialNetwork: Prisma.DbNull,
                locales: Prisma.DbNull,
            },
        })
    }
 
    private async getConfig(scope: ConfigScope, entityId?: string) {
        return this.prisma.configuration.findFirst({
            where: { scope, entityId },
        })
    }
}