All files / src/core/utils utils.ts

14.28% Statements 3/21
0% Branches 0/14
0% Functions 0/6
15.78% Lines 3/19

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    4x                                                                                                                         4x                                                         4x              
import { type User } from '../api/types/identity/User';
 
export const initialStockOperationValue = () => {
  return {
    atLocationName: '',
    atLocationUuid: '',
    cancelReason: '',
    cancelledBy: 0,
    cancelledByFamilyName: '',
    cancelledByGivenName: '',
    cancelledDate: undefined,
    completedBy: 0,
    completedByFamilyName: '',
    completedByGivenName: '',
    completedDate: undefined,
    creator: 0,
    creatorFamilyName: '',
    creatorGivenName: '',
    dateCreated: undefined,
    destinationName: '',
    destinationUuid: '',
    dispatchedByFamilyName: '',
    dispatchedByGivenName: '',
    dispatchedDate: undefined,
    externalReference: '',
    locked: false,
    operationDate: new Date(),
    operationNumber: '',
    operationOrder: 0,
    operationType: '',
    operationTypeName: '',
    operationTypeUuid: '',
    permission: undefined,
    reasonName: '',
    reasonUuid: '',
    rejectedByFamilyName: '',
    rejectedByGivenName: '',
    rejectedDate: undefined,
    rejectionReason: '',
    remarks: '',
    requisitionStockOperationUuid: '',
    responsiblePerson: 0,
    responsiblePersonFamilyName: '',
    responsiblePersonGivenName: '',
    responsiblePersonOther: '',
    responsiblePersonUuid: '',
    returnReason: '',
    returnedByFamilyName: '',
    returnedByGivenName: '',
    returnedDate: undefined,
    sourceName: '',
    sourceUuid: '',
    status: undefined,
    stockOperationItems: [],
    submitted: false,
    submittedBy: '',
    submittedByFamilyName: '',
    submittedByGivenName: '',
    submittedDate: undefined,
    uuid: '',
  };
};
 
export const pick = <T extends object>(obj: T, fields: string[]): Partial<T> => {
  if (!obj || typeof obj !== 'object') {
    throw new TypeError('First argument must be an object');
  }
 
  if (!Array.isArray(fields)) {
    throw new TypeError('Second argument must be an array');
  }
 
  const getNestedValue = (obj: any, path: string): any => {
    return path.split('.').reduce((acc, key) => (acc && acc[key] !== undefined ? acc[key] : undefined), obj);
  };
 
  return fields.reduce((result, field) => {
    const value = getNestedValue(obj, field);
    if (value !== undefined) {
      const keys = field.split('.');
      const lastKey = keys.pop()!;
 
      // Create nested structure in result
      keys.reduce((nested, key) => {
        if (!nested[key]) nested[key] = {};
        return nested[key];
      }, result as any)[lastKey] = value;
    }
    return result;
  }, {} as Partial<T>);
};
 
export const otherUser: User = {
  uuid: 'Other',
  display: 'Other',
  person: {
    display: 'Other',
  },
} as unknown as User;