All files / src/utils SaveDomainUtils.ts

97.61% Statements 41/42
93.75% Branches 15/16
100% Functions 13/13
97.56% Lines 40/41

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                            66x 2x     66x 1x               19x 19x 19x 19x 3x   19x 19x 19x 19x     6x 6x         6x   19x     7x   10x                                                     7x 7x 7x 2x   7x 7x 7x 14x 14x     26x 26x     4x   22x   14x   7x     3x   1x         66x 30x 169x 30x  
import { saveDomain, } from './FetchUtils';
import { SERVER_URL } from '@config/base';
import {
  ColumnType,
  ContinueOperate,
  RecordProps,
  RecordValueType,
  SaveRecordProps,
  TableMetaProps
} from "@props/RecordProps";
import { FieldConfigures, getDestTypeForComponentLookup } from "@kernel/ComponentsConfig";
 
let lastTrigger: number | undefined;
 
export const getLastTrigger = (): number | undefined => {
  return lastTrigger;
};
 
export const setLastTrigger = (trigger: number): void => {
  lastTrigger = trigger;
};
 
// FIXME Change the type of props to a specific type and remove the eslint-disable-next-line
// eslint-disable-next-line  @typescript-eslint/no-explicit-any
export async function callSaveDomainWithCallback(props: any): Promise<any> {
  const {
    values, domainName, callback, continueOperate, columns, options
  } = props;
  const isCreate = (values.id == null);
  const params = new URLSearchParams();
  if (options) {
    params.append('options', JSON.stringify(options));
  }
  const url = `${isCreate ? `${SERVER_URL}/${domainName}` : `${SERVER_URL}/${domainName}/${values.id}`}?${params.toString()}`;
  const method = isCreate ? 'POST' : 'PUT';
  const convertedValue: SaveRecordProps = { ...values };
  columns?.forEach((column: TableMetaProps): void => {
    // 不能直接通过 save 对象的接口传递到后端的所有列类型列表
    // 这些字段是通过别的方式更新,不能直接在接口中传递 null 值到后端
    const shouldNotPassValueTypes: ColumnType[] = ['array', 'comments'];
    Iif (shouldNotPassValueTypes.includes(column.type)) {
      // Remove value of this column from values
      // 或者将这部分逻辑放到 callSaveDomainWithCallback 中
      return;
    }
    convertedValue[column.key] = convertValueForSave(column, values);
  });
  return saveDomain({
    domainName, values: convertedValue, url, method,
    successCallback: (data: SaveRecordProps) => {
      callback?.({ data, continueOperate, status: 'success' });
    }, failCallback: () => {
      callback?.({ continueOperate, status: 'fail' });
    }
  });
}
 
export interface SaveDomainCallbackProps {
  data?: SaveRecordProps;
  continueOperate?: ContinueOperate;
  status: 'success' | 'fail';
}
 
export type SaveDomainCallback = (props: SaveDomainCallbackProps) => void;
 
export interface BatchSaveDomainWithCallbackProps {
  type: 'CREATE' | 'UPDATE';
  values: SaveRecordProps[];
  domainName: string;
  callback: SaveDomainCallback;
  continueOperate?: ContinueOperate;
  columns: TableMetaProps[];
  options?: Record<string, unknown>;
}
 
export async function callBatchSaveDomainWithCallback(props: BatchSaveDomainWithCallbackProps): Promise<RecordProps[]> {
  const {
    type, values, domainName, callback,
    continueOperate, columns, options
  } = props;
  const params = new URLSearchParams();
  if (options) {
    params.append('options', JSON.stringify(options));
  }
  const url = `${SERVER_URL}/${domainName}/batch?${params.toString()}`;
  const method = type === 'CREATE' ? 'POST' : 'PUT';
  const convertedValues = values.map((value: SaveRecordProps) => {
    const convertedValue: SaveRecordProps = {};
    columns?.forEach((column: TableMetaProps): void => {
      // 不能直接通过 save 对象的接口传递到后端的所有列类型列表
      // 这些字段是通过别的方式更新,不能直接在接口中传递 null 值到后端
      const shouldNotPassValueTypes: ColumnType[] = ['array', 'comments'];
      if (shouldNotPassValueTypes.includes(column.type)) {
        // Remove value of this column from values
        // 或者将这部分逻辑放到 callSaveDomainWithCallback 中
        return;
      }
      convertedValue[column.key] = convertValueForSave(column, value);
    });
    return convertedValue;
  });
  return saveDomain({
    domainName, values: convertedValues, url, method,
    successCallback: (data: SaveRecordProps) => {
      callback?.({ data, continueOperate, status: 'success' });
    }, failCallback: () => {
      callback?.({ continueOperate, status: 'fail' });
    }
  });
}
 
export const convertValueForSave = (column: TableMetaProps, values: SaveRecordProps): RecordValueType => {
  const destType = getDestTypeForComponentLookup(column.type);
  const frontendValueConverter = FieldConfigures.find(fc => fc.type === destType)?.frontendValueConverter;
  return frontendValueConverter ? frontendValueConverter(values, values[column.key]) : values[column.key];
};