All files / api/module/community communityFileHandler.ts

25% Statements 14/56
100% Branches 0/0
0% Functions 0/1
25% Lines 14/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 1x 1x 1x 1x 1x 1x                                                                                      
import { handleImageFromFormData } from '@api/facade/handleImageFromFormData';
import { FormDataKey } from '@constants/form';
import { BioMediaImageSize, DefaultImageRestrictions, DefaultImageSize } from '@constants/image';
import { IDatabaseFile } from '@contracts/databaseFile';
import { IFormWithFiles } from '@contracts/file';
import { ResultWithValue } from '@contracts/resultWithValue';
import { makeArrayOrDefault } from '@helpers/arrayHelper';
 
export interface ICommunityImages {
  profilePicFile?: IDatabaseFile;
  bioMediaFiles?: Array<IDatabaseFile>;
}
 
export const communityFileHandler = async (
  formData: IFormWithFiles,
): Promise<ResultWithValue<ICommunityImages>> => {
  const result: ICommunityImages = {
    bioMediaFiles: [],
  };

  const profilePicResult = await handleImageFromFormData({
    fileFromForm: formData[FormDataKey.profilePicFile],
    restrictions: DefaultImageRestrictions.profilePic,
    fileName: 'profilePic',
    handlerName: 'communityFileHandler',
    ...DefaultImageSize,
  });
  if (profilePicResult.isSuccess == false) {
    return { ...profilePicResult, value: result };
  }
  result.profilePicFile = profilePicResult.value;

  // ---

  const bioMediaFilesFromForm = formData[FormDataKey.bioMediaFiles];
  if (result.bioMediaFiles == null) result.bioMediaFiles = [];
  for (const bioMediaFileFromForm of makeArrayOrDefault(bioMediaFilesFromForm)) {
    const bioMediaFileResult = await handleImageFromFormData({
      fileFromForm: bioMediaFileFromForm,
      restrictions: DefaultImageRestrictions.bioMediaPic,
      fileName: 'bioMediaPic',
      handlerName: 'communityFileHandler',
      ...BioMediaImageSize,
    });
    if (bioMediaFileResult.isSuccess == false) {
      continue;
    }
    result.bioMediaFiles.push(bioMediaFileResult.value);
  }

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