forked from Ivasoft/mattermost-mobile
* Allow database operator handlers to deal with empty or undefined input values * Prevent known handler warnings * Update app/database/operator/server_data_operator/handlers/post.ts Co-authored-by: Avinash Lingaloo <avinashlng1080@gmail.com> * feedback review * remove unnecessary ! Co-authored-by: Avinash Lingaloo <avinashlng1080@gmail.com>
52 lines
1.9 KiB
TypeScript
52 lines
1.9 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {MM_TABLES} from '@constants/database';
|
|
import {buildAppInfoKey} from '@database/operator/app_data_operator/comparator';
|
|
import {transformInfoRecord, transformGlobalRecord} from '@database/operator/app_data_operator/transformers';
|
|
import BaseDataOperator from '@database/operator/base_data_operator';
|
|
import {getUniqueRawsBy} from '@database/operator/utils/general';
|
|
|
|
import type {HandleInfoArgs, HandleGlobalArgs} from '@typings/database/database';
|
|
|
|
const {APP: {INFO, GLOBAL}} = MM_TABLES;
|
|
|
|
export default class AppDataOperator extends BaseDataOperator {
|
|
handleInfo = async ({info, prepareRecordsOnly = true}: HandleInfoArgs) => {
|
|
if (!info?.length) {
|
|
// eslint-disable-next-line no-console
|
|
console.warn(
|
|
'An empty or undefined "info" array has been passed to the handleInfo',
|
|
);
|
|
return [];
|
|
}
|
|
|
|
return this.handleRecords({
|
|
fieldName: 'version_number',
|
|
buildKeyRecordBy: buildAppInfoKey,
|
|
transformer: transformInfoRecord,
|
|
prepareRecordsOnly,
|
|
createOrUpdateRawValues: getUniqueRawsBy({raws: info, key: 'version_number'}),
|
|
tableName: INFO,
|
|
});
|
|
};
|
|
|
|
handleGlobal = async ({globals, prepareRecordsOnly = true}: HandleGlobalArgs) => {
|
|
if (!globals?.length) {
|
|
// eslint-disable-next-line no-console
|
|
console.warn(
|
|
'An empty or undefined "globals" array has been passed to the handleGlobal',
|
|
);
|
|
return [];
|
|
}
|
|
|
|
return this.handleRecords({
|
|
fieldName: 'id',
|
|
transformer: transformGlobalRecord,
|
|
prepareRecordsOnly,
|
|
createOrUpdateRawValues: getUniqueRawsBy({raws: globals, key: 'id'}),
|
|
tableName: GLOBAL,
|
|
});
|
|
};
|
|
}
|