All files / src/domain Water.ts

88% Statements 22/25
100% Branches 0/0
100% Functions 3/3
88% Lines 22/25

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                                                                                                        1x                           1x 1x 1x 1x 1x       1x 1x 1x   1x         1x 1x         1x         1x 1x 1x         1x               1x               1x 1x 1x   1x                        
import { SecretString } from "lib";
import { Env } from "../Env";
import Logger from "bunyan";
 
export interface FetchSettingModel {
  id: bigint;
  siteId: bigint;
  userName: string;
  password: SecretString;
}
 
export interface MonthlyUsageModel {
  fetchSettingId: bigint;
  year: number;
  month: number;
  begin: string | null;
  end: string;
  amount: number;
  yen: number;
}
 
export interface UsageFetcher {
  fetchMonthly(
    logger: Logger,
    setting: FetchSettingModel
  ): Promise<MonthlyUsageModel[]>;
}
 
export interface FetchSettingRepository {
  findAllWaterFetchSettings(): Promise<FetchSettingModel[]>;
}
 
export interface UsageRepository {
  saveWaterMonthlyUsages(usages: MonthlyUsageModel[], now: Date): Promise<void>;
}
 
export interface FetchStatusRepository {
  upsertWaterFetchStatusSuccess(
    fetchSettingId: bigint,
    now: Date
  ): Promise<void>;
  upsertWaterFetchStatusFailure(
    fetchSettingId: bigint,
    now: Date
  ): Promise<void>;
}
 
export interface RunResult {
  successfulCount: number;
  failureCount: number;
}
 
export class UsageService {
  readonly env: Env;
  readonly fetcher: UsageFetcher;
  readonly fetchSettingRepo: FetchSettingRepository;
  readonly usageRepo: UsageRepository;
  readonly fetchStatusRepo: FetchStatusRepository;
 
  constructor(
    env: Env,
    fetcher: UsageFetcher,
    fetchSettingRepo: FetchSettingRepository,
    usageRepo: UsageRepository,
    fetchStatusRepo: FetchStatusRepository
  ) {
    this.env = env;
    this.fetcher = fetcher;
    this.fetchSettingRepo = fetchSettingRepo;
    this.usageRepo = usageRepo;
    this.fetchStatusRepo = fetchStatusRepo;
  }
 
  async run(parentLogger: Logger): Promise<RunResult> {
    const now = new Date();
    const settings = await this.fetchSettingRepo.findAllWaterFetchSettings();
    parentLogger.info("loaded water_fetch_settings, count %d", settings.length);
 
    const result = {
      successfulCount: 0,
      failureCount: 0,
    };
 
    for (const setting of settings) {
      const logger = parentLogger.child({
        setting_id: setting.id.toString(),
        user_name: setting.userName,
      });
 
      logger.info(
        "fetch water start by setting[%s, %s]",
        setting.id.toString(),
        setting.userName
      );
      try {
        await this.fetchAndSave(logger, now, setting);
        result.successfulCount++;
      } catch (err) {
        logger.error({ err }, "fetch water failed");
        result.failureCount++;
      } finally {
        logger.info(
          "fetch water end by setting[%s, %s]",
          setting.id.toString(),
          setting.userName
        );
      }
    }
 
    return result;
  }
 
  private async fetchAndSave(
    logger: Logger,
    now: Date,
    setting: FetchSettingModel
  ): Promise<void> {
    try {
      const usages = await this.fetcher.fetchMonthly(logger, setting);
      await this.usageRepo.saveWaterMonthlyUsages(usages, now);
 
      await this.fetchStatusRepo.upsertWaterFetchStatusSuccess(
        setting.id,
        new Date()
      );
    } catch (err) {
      await this.fetchStatusRepo.upsertWaterFetchStatusFailure(
        setting.id,
        new Date()
      );
    }
  }
}