File

apps/recallassess/recallassess-api/src/api/admin/testimonial/testimonial.service.ts

Extends

BNestBaseModuleService

Index

Methods

Constructor

constructor(prisma: BNestPrismaService, systemLogService: SystemLogService)
Parameters :
Name Type Optional
prisma BNestPrismaService No
systemLogService SystemLogService No

Methods

Async add
add(data: any)

Override add method to log creation

Parameters :
Name Type Optional
data any No
Returns : Promise<any>
Async delete
delete(id: number)

Override delete method to log deletion

Parameters :
Name Type Optional
id number No
Returns : Promise<void>
Async save
save(id: number, data: any)

Override save method to log update

Parameters :
Name Type Optional
id number No
data any No
Returns : Promise<any>
import { BNestBaseModuleService } from "@bish-nest/core/data/module-service/base-module.service";
import { BNestPrismaService } from "@bish-nest/core/services";
import { SystemLogService } from "@api/shared/services";
import { Injectable, NotFoundException } from "@nestjs/common";
import { SystemLogEntityType } from "@prisma/client";

@Injectable()
export class TestimonialService extends BNestBaseModuleService {
  constructor(
    protected prisma: BNestPrismaService,
    private readonly systemLogService: SystemLogService,
  ) {
    super();
  }

  /**
   * Override add method to log creation
   */
  async add(data: any): Promise<any> {
    const addResponse = await super.add(data);
    const testimonial = addResponse.data;

    // Log the creation
    await this.systemLogService.logInsert(
      SystemLogEntityType.TESTIMONIAL,
      testimonial.id,
      testimonial as Record<string, unknown>,
    );

    return addResponse;
  }

  /**
   * Override save method to log update
   */
  async save(id: number, data: any): Promise<any> {
    // Get old data before update
    const oldTestimonial = await this.prisma.client.testimonial.findUnique({
      where: { id },
    });

    if (!oldTestimonial) {
      throw new NotFoundException(`Testimonial with ID ${id} not found`);
    }

    const saveResponse = await super.save(id, data);
    const updatedTestimonial = saveResponse.data;

    // Calculate changed fields and log the update
    const changedFields = SystemLogService.calculateChangedFields(
      oldTestimonial as Record<string, unknown>,
      updatedTestimonial as Record<string, unknown>,
    );

    await this.systemLogService.logUpdate(
      SystemLogEntityType.TESTIMONIAL,
      id,
      oldTestimonial as Record<string, unknown>,
      updatedTestimonial as Record<string, unknown>,
      changedFields,
    );

    return saveResponse;
  }

  /**
   * Override delete method to log deletion
   */
  async delete(id: number): Promise<void> {
    // Get testimonial data before deletion
    const testimonial = await this.prisma.client.testimonial.findUnique({
      where: { id },
    });

    if (!testimonial) {
      throw new NotFoundException(`Testimonial with ID ${id} not found`);
    }

    // Call parent delete method
    await super.delete(id);

    // Log the deletion
    await this.systemLogService.logDelete(
      SystemLogEntityType.TESTIMONIAL,
      id,
      testimonial as Record<string, unknown>,
    );
  }
}

results matching ""

    No results matching ""