All files / src/infra MySqlClient.ts

100% Statements 135/135
100% Branches 8/8
100% Functions 7/7
100% Lines 135/135

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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 1361x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 1x  
import { AppStatusRepository } from "../domain/types/AppStatus";
import {
  LineChannel,
  LineChannelRepository,
  LineUserRepository,
} from "../domain/types/Line";
import { PrismaClient } from "@prisma/client";
 
export class MySqlClient
  implements AppStatusRepository, LineChannelRepository, LineUserRepository
{
  constructor(private prisma: PrismaClient) {}
  async upsertAppStatusStopped(appName: string, now: Date): Promise<void> {
    await this.prisma.app_statuses.upsert({
      where: {
        app_name: appName,
      },
      create: {
        app_name: appName,
        app_status_types: {
          connect: {
            type_name: "stopped",
          },
        },
        last_successful_at: now,
      },
      update: {
        app_status_types: {
          connect: {
            type_name: "stopped",
          },
        },
        last_successful_at: now,
        updated_at: now,
      },
    });
  }
 
  async upsertAppStatusRunning(appName: string, now: Date): Promise<void> {
    await this.prisma.app_statuses.upsert({
      where: {
        app_name: appName,
      },
      create: {
        app_name: appName,
        app_status_types: {
          connect: {
            type_name: "running",
          },
        },
      },
      update: {
        app_status_types: {
          connect: {
            type_name: "running",
          },
        },
        updated_at: now,
      },
    });
  }
 
  async upsertAppStatusError(appName: string, now: Date): Promise<void> {
    await this.prisma.app_statuses.upsert({
      where: {
        app_name: appName,
      },
      create: {
        app_name: appName,
        app_status_types: {
          connect: {
            type_name: "error",
          },
        },
        last_failure_at: now,
      },
      update: {
        app_status_types: {
          connect: {
            type_name: "error",
          },
        },
        last_failure_at: now,
        updated_at: now,
      },
    });
  }
 
  async fetchAllLineChannels(): Promise<LineChannel[]> {
    return await this.prisma.line_channels.findMany();
  }
 
  async addLineUserIfNotExists(userId: string): Promise<void> {
    await this.prisma.line_users.upsert({
      where: { id: userId },
      update: {},
      create: {
        id: userId,
      },
    });
  }
 
  async upsertElectricityNotifySetting(
    channelId: string,
    userId: string,
    enable: boolean
  ): Promise<number> {
    const settings = await this.prisma.electricity_notify_settings.findMany({
      where: {
        line_channel_id: channelId,
      },
    });
 
    for (const setting of settings) {
      await this.prisma.electricity_notify_dest_line_users.upsert({
        where: {
          electricity_notify_setting_id_line_user_id: {
            electricity_notify_setting_id: setting.id,
            line_user_id: userId,
          },
        },
        update: {
          notify_enable: enable,
        },
        create: {
          electricity_notify_setting_id: setting.id,
          line_user_id: userId,
          notify_enable: enable,
        },
      });
    }
 
    return settings.length;
  }
}