All files / api/module/planetBuild planetBuildRelationshipsHandler.ts

0% Statements 0/58
0% Branches 0/1
0% Functions 0/1
0% Lines 0/58

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                                                                                                                     
import { PlanetBuildDto } from '@contracts/dto/forms/planetBuildDto';
import { Result, ResultWithValue } from '@contracts/resultWithValue';
import { getDatabaseService } from '@services/external/database/databaseService';
import { PlanetBuild } from '@services/external/database/xata';

export const planetBuildCreateRelationships = async (
  dto: PlanetBuildDto,
  persistence: PlanetBuild,
): Promise<Result> => {
  const itemsToCreate: Array<{ planetBuild: string; builder: string }> = [];
  for (const builder of dto.builders) {
    itemsToCreate.push({
      planetBuild: persistence.id,
      builder: builder,
    });
  }

  try {
    await getDatabaseService().planetBuildBuilder().table.create(itemsToCreate);
  } catch (ex) {
    return {
      isSuccess: false,
      errorMessage: ex?.toString?.() ?? 'planetBuildCreateRelationships exception',
    };
  }

  return {
    isSuccess: true,
    errorMessage: '',
  };
};

export const planetBuildAdditionalPropertiesToDto = async (
  id: string,
  dto: PlanetBuildDto,
): Promise<ResultWithValue<PlanetBuildDto>> => {
  const result = { ...dto };
  const joinTableRecordsResult = await getDatabaseService() //
    .planetBuildBuilder()
    .getByBuilderId(id);
  if (joinTableRecordsResult.isSuccess === false) {
    return {
      isSuccess: false,
      value: dto,
      errorMessage: joinTableRecordsResult.errorMessage,
    };
  }

  result.builders = joinTableRecordsResult.value
    .map((cb) => cb.builder?.id ?? '')
    .filter((cb) => cb.length > 0); // get all ids with length not 0

  return {
    isSuccess: true,
    value: result,
    errorMessage: '',
  };
};