All files / api/module/builder builderRelationshipsHandler.ts

14.28% Statements 8/56
100% Branches 0/0
0% Functions 0/2
14.28% Lines 8/56

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 571x 1x 1x 1x 1x 1x                                                   1x 1x                                                
import { BuilderDto } from '@contracts/dto/forms/builderDto';
import { Result, ResultWithValue } from '@contracts/resultWithValue';
import { getDatabaseService } from '@services/external/database/databaseService';
import { Builder } from '@services/external/database/xata';
 
export const builderCreateRelationships = async (
  dto: BuilderDto,
  persistence: Builder,
): Promise<Result> => {
  const itemsToCreate: Array<{ builder: string; community: string }> = [];
  for (const commAffiliations of dto.communityAffiliations) {
    itemsToCreate.push({
      builder: persistence.id,
      community: commAffiliations,
    });
  }

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

  return {
    isSuccess: true,
    errorMessage: '',
  };
};
 
export const builderAdditionalPropertiesToDto = async (
  id: string,
  dto: BuilderDto,
): Promise<ResultWithValue<BuilderDto>> => {
  const result = { ...dto };
  const comBuiRecordsResult = await getDatabaseService().communityBuilder().getByBuilderId(id);
  if (comBuiRecordsResult.isSuccess === false) {
    return {
      isSuccess: false,
      value: dto,
      errorMessage: comBuiRecordsResult.errorMessage,
    };
  }

  result.communityAffiliations = comBuiRecordsResult.value
    .map((cb) => cb.community?.id ?? '')
    .filter((cb) => cb.length > 0); // get all ids with length not 0

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