MM_30482 : Refactoring DataOperator class [ IN PROGRESS ]

This commit is contained in:
Avinash Lingaloo
2021-04-09 19:26:00 +04:00
parent f0c660a715
commit cb0d8329b3
4 changed files with 647 additions and 382 deletions

View File

@@ -42,7 +42,13 @@ import {
} from '@database/operator/prepareRecords/user';
import DatabaseManager from '@database/manager';
import DataOperatorException from '@database/exceptions/data_operator_exception';
import {RawApp, RawGlobal, RawRole, RawServers, RawTermsOfService} from '@typings/database/database';
import {
RawApp,
RawGlobal,
RawRole,
RawServers,
RawTermsOfService,
} from '@typings/database/database';
import {DatabaseType, IsolatedEntities} from '@typings/database/enums';
import {
@@ -101,7 +107,11 @@ describe('*** DataOperator: Handlers tests ***', () => {
},
];
await DataOperator.handleIsolatedEntity({tableName: IsolatedEntities.APP, values});
await DataOperator.handleIsolatedEntity({
tableName: IsolatedEntities.APP,
values,
prepareRecordsOnly: false,
});
expect(spyOnHandleEntityRecords).toHaveBeenCalledWith({
fieldName: 'version_number',
@@ -127,7 +137,11 @@ describe('*** DataOperator: Handlers tests ***', () => {
const spyOnHandleEntityRecords = jest.spyOn(DataOperator as any, 'handleEntityRecords');
const values: RawGlobal[] = [{name: 'global-1-name', value: 'global-1-value'}];
await DataOperator.handleIsolatedEntity({tableName: IsolatedEntities.GLOBAL, values});
await DataOperator.handleIsolatedEntity({
tableName: IsolatedEntities.GLOBAL,
values,
prepareRecordsOnly: false,
});
expect(spyOnHandleEntityRecords).toHaveBeenCalledWith({
findMatchingRecordBy: isRecordGlobalEqualToRaw,
@@ -145,6 +159,7 @@ describe('*** DataOperator: Handlers tests ***', () => {
expect(defaultDB).toBeTruthy();
const spyOnHandleEntityRecords = jest.spyOn(DataOperator as any, 'handleEntityRecords');
const values: RawServers[] = [
{
db_path: 'server.db',
@@ -154,7 +169,12 @@ describe('*** DataOperator: Handlers tests ***', () => {
url: 'https://community.mattermost.com',
},
];
await DataOperator.handleIsolatedEntity({tableName: IsolatedEntities.SERVERS, values});
await DataOperator.handleIsolatedEntity({
tableName: IsolatedEntities.SERVERS,
values,
prepareRecordsOnly: false,
});
expect(spyOnHandleEntityRecords).toHaveBeenCalledWith({
fieldName: 'url',
@@ -180,6 +200,7 @@ describe('*** DataOperator: Handlers tests ***', () => {
expect(database).toBeTruthy();
const spyOnHandleEntityRecords = jest.spyOn(DataOperator as any, 'handleEntityRecords');
const values: RawRole[] = [
{
id: 'custom-emoji-id-1',
@@ -191,6 +212,7 @@ describe('*** DataOperator: Handlers tests ***', () => {
await DataOperator.handleIsolatedEntity({
tableName: IsolatedEntities.ROLE,
values,
prepareRecordsOnly: false,
});
expect(spyOnHandleEntityRecords).toHaveBeenCalledWith({
@@ -215,8 +237,14 @@ describe('*** DataOperator: Handlers tests ***', () => {
expect(database).toBeTruthy();
const spyOnHandleEntityRecords = jest.spyOn(DataOperator as any, 'handleEntityRecords');
const values = [{id: 'system-id-1', name: 'system-1', value: 'system-1'}];
await DataOperator.handleIsolatedEntity({tableName: IsolatedEntities.SYSTEM, values});
await DataOperator.handleIsolatedEntity({
tableName: IsolatedEntities.SYSTEM,
values,
prepareRecordsOnly: false,
});
expect(spyOnHandleEntityRecords).toHaveBeenCalledWith({
findMatchingRecordBy: isRecordSystemEqualToRaw,
@@ -248,6 +276,7 @@ describe('*** DataOperator: Handlers tests ***', () => {
await DataOperator.handleIsolatedEntity({
tableName: IsolatedEntities.TERMS_OF_SERVICE,
values,
prepareRecordsOnly: false,
});
expect(spyOnHandleEntityRecords).toHaveBeenCalledWith({
@@ -297,7 +326,7 @@ describe('*** DataOperator: Handlers tests ***', () => {
user_id: 'ooumoqgq3bfiijzwbn8badznwc',
},
],
prepareRowsOnly: false,
prepareRecordsOnly: false,
});
// Called twice: Once for Reaction record and once for CustomEmoji record
@@ -339,7 +368,7 @@ describe('*** DataOperator: Handlers tests ***', () => {
},
];
await DataOperator.handleDraft(values);
await DataOperator.handleDraft({drafts: values, prepareRecordsOnly: false});
expect(spyOnHandleEntityRecords).toHaveBeenCalledWith({
findMatchingRecordBy: isRecordDraftEqualToRaw,
@@ -378,7 +407,7 @@ describe('*** DataOperator: Handlers tests ***', () => {
has_preview_image: false,
},
],
prepareRowsOnly: false,
prepareRecordsOnly: false,
});
expect(spyOnPrepareRecords).toHaveBeenCalledTimes(1);
@@ -607,12 +636,15 @@ describe('*** DataOperator: Handlers tests ***', () => {
embed: [
{
type: 'opengraph',
url: 'https://github.com/mickmister/mattermost-plugin-default-theme',
url:
'https://github.com/mickmister/mattermost-plugin-default-theme',
data: {
type: 'object',
url: 'https://github.com/mickmister/mattermost-plugin-default-theme',
url:
'https://github.com/mickmister/mattermost-plugin-default-theme',
title: 'mickmister/mattermost-plugin-default-theme',
description: 'Contribute to mickmister/mattermost-plugin-default-theme development by creating an account on GitHub.',
description:
'Contribute to mickmister/mattermost-plugin-default-theme development by creating an account on GitHub.',
determiner: '',
site_name: 'GitHub',
locale: '',
@@ -620,7 +652,8 @@ describe('*** DataOperator: Handlers tests ***', () => {
images: [
{
url: '',
secure_url: 'https://community-release.mattermost.com/api/v4/image?url=https%3A%2F%2Favatars1.githubusercontent.com%2Fu%2F6913320%3Fs%3D400%26v%3D4',
secure_url:
'https://community-release.mattermost.com/api/v4/image?url=https%3A%2F%2Favatars1.githubusercontent.com%2Fu%2F6913320%3Fs%3D400%26v%3D4',
type: '',
width: 0,
height: 0,
@@ -704,7 +737,7 @@ describe('*** DataOperator: Handlers tests ***', () => {
channel: true,
auto_responder_active: false,
auto_responder_message:
'Hello, I am out of office and unable to respond to messages.',
'Hello, I am out of office and unable to respond to messages.',
comments: 'never',
desktop_notification_sound: 'Hello',
push_status: 'online',
@@ -720,14 +753,11 @@ describe('*** DataOperator: Handlers tests ***', () => {
},
];
const spyOnExecuteInDatabase = jest.spyOn(
DataOperator as any,
'executeInDatabase',
);
const spyOnExecuteInDatabase = jest.spyOn(DataOperator as any, 'executeInDatabase');
await createConnection(true);
await DataOperator.handleUsers(users);
await DataOperator.handleUsers({users, prepareRecordsOnly: false});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -759,7 +789,7 @@ describe('*** DataOperator: Handlers tests ***', () => {
channel: true,
auto_responder_active: false,
auto_responder_message:
'Hello, I am out of office and unable to respond to messages.',
'Hello, I am out of office and unable to respond to messages.',
comments: 'never',
desktop_notification_sound: 'Hello',
push_status: 'online',
@@ -816,7 +846,7 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handlePreferences(preferences);
await DataOperator.handlePreferences({preferences, prepareRecordsOnly: false});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -843,7 +873,7 @@ describe('*** DataOperator: Handlers tests ***', () => {
category: 'theme',
name: '',
value:
'{"awayIndicator":"#c1b966","buttonBg":"#4cbba4","buttonColor":"#ffffff","centerChannelBg":"#2f3e4e","centerChannelColor":"#dddddd","codeTheme":"solarized-dark","dndIndicator":"#e81023","errorTextColor":"#ff6461","image":"/static/files/0b8d56c39baf992e5e4c58d74fde0fd6.png","linkColor":"#a4ffeb","mentionBg":"#b74a4a","mentionColor":"#ffffff","mentionHighlightBg":"#984063","mentionHighlightLink":"#a4ffeb","newMessageSeparator":"#5de5da","onlineIndicator":"#65dcc8","sidebarBg":"#1b2c3e","sidebarHeaderBg":"#1b2c3e","sidebarHeaderTextColor":"#ffffff","sidebarText":"#ffffff","sidebarTextActiveBorder":"#66b9a7","sidebarTextActiveColor":"#ffffff","sidebarTextHoverBg":"#4a5664","sidebarUnreadText":"#ffffff","type":"Mattermost Dark"}',
'{"awayIndicator":"#c1b966","buttonBg":"#4cbba4","buttonColor":"#ffffff","centerChannelBg":"#2f3e4e","centerChannelColor":"#dddddd","codeTheme":"solarized-dark","dndIndicator":"#e81023","errorTextColor":"#ff6461","image":"/static/files/0b8d56c39baf992e5e4c58d74fde0fd6.png","linkColor":"#a4ffeb","mentionBg":"#b74a4a","mentionColor":"#ffffff","mentionHighlightBg":"#984063","mentionHighlightLink":"#a4ffeb","newMessageSeparator":"#5de5da","onlineIndicator":"#65dcc8","sidebarBg":"#1b2c3e","sidebarHeaderBg":"#1b2c3e","sidebarHeaderTextColor":"#ffffff","sidebarText":"#ffffff","sidebarTextActiveBorder":"#66b9a7","sidebarTextActiveColor":"#ffffff","sidebarTextHoverBg":"#4a5664","sidebarUnreadText":"#ffffff","type":"Mattermost Dark"}',
},
},
{
@@ -864,7 +894,7 @@ describe('*** DataOperator: Handlers tests ***', () => {
it('=> HandleTeamMemberships: should write to TEAM_MEMBERSHIP entity', async () => {
expect.assertions(2);
const teamMembership = [
const teamMemberships = [
{
team_id: 'a',
user_id: 'ab',
@@ -877,14 +907,14 @@ describe('*** DataOperator: Handlers tests ***', () => {
},
];
const spyOnExecuteInDatabase = jest.spyOn(
DataOperator as any,
'executeInDatabase',
);
const spyOnExecuteInDatabase = jest.spyOn(DataOperator as any, 'executeInDatabase');
await createConnection(true);
await DataOperator.handleTeamMemberships(teamMembership);
await DataOperator.handleTeamMemberships({
teamMemberships,
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -921,16 +951,14 @@ describe('*** DataOperator: Handlers tests ***', () => {
},
];
const spyOnExecuteInDatabase = jest.spyOn(
DataOperator as any,
'executeInDatabase',
);
const spyOnExecuteInDatabase = jest.spyOn(DataOperator as any, 'executeInDatabase');
await createConnection(true);
await DataOperator.handleIsolatedEntity({
tableName: IsolatedEntities.CUSTOM_EMOJI,
values: emojis,
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
@@ -966,7 +994,10 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleGroupMembership(groupMemberships);
await DataOperator.handleGroupMembership({
groupMemberships,
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1033,7 +1064,10 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleChannelMembership(channelMemberships);
await DataOperator.handleChannelMembership({
channelMemberships,
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1092,27 +1126,27 @@ describe('*** DataOperator: Handlers tests ***', () => {
it('=> HandleGroup: should write to GROUP entity', async () => {
expect.assertions(2);
const spyOnExecuteInDatabase = jest.spyOn(
DataOperator as any,
'executeInDatabase',
);
const spyOnExecuteInDatabase = jest.spyOn(DataOperator as any, 'executeInDatabase');
await createConnection(true);
await DataOperator.handleGroup([
{
id: 'id_groupdfjdlfkjdkfdsf',
name: 'mobile_team',
display_name: 'mobile team',
description: '',
source: '',
remote_id: '',
create_at: 0,
update_at: 0,
delete_at: 0,
has_syncables: true,
},
]);
await DataOperator.handleGroup({
groups: [
{
id: 'id_groupdfjdlfkjdkfdsf',
name: 'mobile_team',
display_name: 'mobile team',
description: '',
source: '',
remote_id: '',
create_at: 0,
update_at: 0,
delete_at: 0,
has_syncables: true,
},
],
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1145,18 +1179,21 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleGroupsInTeam([
{
team_id: 'team_899',
team_display_name: '',
team_type: '',
group_id: 'group_id89',
auto_add: true,
create_at: 0,
delete_at: 0,
update_at: 0,
},
]);
await DataOperator.handleGroupsInTeam({
groupsInTeams: [
{
team_id: 'team_899',
team_display_name: '',
team_type: '',
group_id: 'group_id89',
auto_add: true,
create_at: 0,
delete_at: 0,
update_at: 0,
},
],
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1187,23 +1224,26 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleGroupsInChannel([
{
auto_add: true,
channel_display_name: '',
channel_id: 'channelid',
channel_type: '',
create_at: 0,
delete_at: 0,
group_id: 'groupId',
team_display_name: '',
team_id: '',
team_type: '',
update_at: 0,
member_count: 0,
timezone_count: 0,
},
]);
await DataOperator.handleGroupsInChannel({
groupsInChannels: [
{
auto_add: true,
channel_display_name: '',
channel_id: 'channelid',
channel_type: '',
create_at: 0,
delete_at: 0,
group_id: 'groupId',
team_display_name: '',
team_id: '',
team_type: '',
update_at: 0,
member_count: 0,
timezone_count: 0,
},
],
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1239,26 +1279,29 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleTeam([
{
id: 'rcgiyftm7jyrxnmdfdfa1osd8zswby',
create_at: 1445538153952,
update_at: 1588876392150,
delete_at: 0,
display_name: 'Contributors',
name: 'core',
description: '',
email: '',
type: 'O',
company_name: '',
allowed_domains: '',
invite_id: 'codoy5s743rq5mk18i7u5dfdfksz7e',
allow_open_invite: true,
last_team_icon_update: 1525181587639,
scheme_id: 'hbwgrncq1pfcdkpotzidfdmarn95o',
group_constrained: null,
},
]);
await DataOperator.handleTeam({
teams: [
{
id: 'rcgiyftm7jyrxnmdfdfa1osd8zswby',
create_at: 1445538153952,
update_at: 1588876392150,
delete_at: 0,
display_name: 'Contributors',
name: 'core',
description: '',
email: '',
type: 'O',
company_name: '',
allowed_domains: '',
invite_id: 'codoy5s743rq5mk18i7u5dfdfksz7e',
allow_open_invite: true,
last_team_icon_update: 1525181587639,
scheme_id: 'hbwgrncq1pfcdkpotzidfdmarn95o',
group_constrained: null,
},
],
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1297,12 +1340,15 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleTeamChannelHistory([
{
team_id: 'a',
channel_ids: ['ca', 'cb'],
},
]);
await DataOperator.handleTeamChannelHistory({
teamChannelHistories: [
{
team_id: 'a',
channel_ids: ['ca', 'cb'],
},
],
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1320,26 +1366,29 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleTeam([
{
id: 'rcgiyftm7jyrxnmdfdfa1osd8zswby',
create_at: 1445538153952,
update_at: 1588876392150,
delete_at: 0,
display_name: 'Contributors',
name: 'core',
description: '',
email: '',
type: 'O',
company_name: '',
allowed_domains: '',
invite_id: 'codoy5s743rq5mk18i7u5dfdfksz7e',
allow_open_invite: true,
last_team_icon_update: 1525181587639,
scheme_id: 'hbwgrncq1pfcdkpotzidfdmarn95o',
group_constrained: null,
},
]);
await DataOperator.handleTeam({
teams: [
{
id: 'rcgiyftm7jyrxnmdfdfa1osd8zswby',
create_at: 1445538153952,
update_at: 1588876392150,
delete_at: 0,
display_name: 'Contributors',
name: 'core',
description: '',
email: '',
type: 'O',
company_name: '',
allowed_domains: '',
invite_id: 'codoy5s743rq5mk18i7u5dfdfksz7e',
allow_open_invite: true,
last_team_icon_update: 1525181587639,
scheme_id: 'hbwgrncq1pfcdkpotzidfdmarn95o',
group_constrained: null,
},
],
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1378,12 +1427,15 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleTeamChannelHistory([
{
team_id: 'a',
channel_ids: ['ca', 'cb'],
},
]);
await DataOperator.handleTeamChannelHistory({
teamChannelHistories: [
{
team_id: 'a',
channel_ids: ['ca', 'cb'],
},
],
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1401,14 +1453,17 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleTeamSearchHistory([
{
team_id: 'a',
term: 'termA',
display_term: 'termA',
created_at: 1445538153952,
},
]);
await DataOperator.handleTeamSearchHistory({
teamSearchHistories: [
{
team_id: 'a',
term: 'termA',
display_term: 'termA',
created_at: 1445538153952,
},
],
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1435,27 +1490,30 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleSlashCommand([
{
id: 'command_1',
auto_complete: true,
auto_complete_desc: 'mock_command',
auto_complete_hint: 'hint',
create_at: 1445538153952,
creator_id: 'creator_id',
delete_at: 1445538153952,
description: 'description',
display_name: 'display_name',
icon_url: 'display_name',
method: 'get',
team_id: 'teamA',
token: 'token',
trigger: 'trigger',
update_at: 1445538153953,
url: 'url',
username: 'userA',
},
]);
await DataOperator.handleSlashCommand({
slashCommands: [
{
id: 'command_1',
auto_complete: true,
auto_complete_desc: 'mock_command',
auto_complete_hint: 'hint',
create_at: 1445538153952,
creator_id: 'creator_id',
delete_at: 1445538153952,
description: 'description',
display_name: 'display_name',
icon_url: 'display_name',
method: 'get',
team_id: 'teamA',
token: 'token',
trigger: 'trigger',
update_at: 1445538153953,
url: 'url',
username: 'userA',
},
],
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1495,14 +1553,17 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleMyTeam([
{
team_id: 'teamA',
roles: 'roleA, roleB, roleC',
is_unread: true,
mentions_count: 3,
},
]);
await DataOperator.handleMyTeam({
myTeams: [
{
team_id: 'teamA',
roles: 'roleA, roleB, roleC',
is_unread: true,
mentions_count: 3,
},
],
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1529,28 +1590,31 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleChannel([
{
id: 'kjlw9j1ttnxwig7tnqgebg7dtipno',
create_at: 1600185541285,
update_at: 1604401077256,
delete_at: 0,
team_id: '',
type: 'D',
display_name: '',
name: 'gh781zkzkhh357b4bejephjz5u8daw__9ciscaqbrpd6d8s68k76xb9bte',
header: '(https://mattermost',
purpose: '',
last_post_at: 1617311494451,
total_msg_count: 585,
extra_update_at: 0,
creator_id: '',
scheme_id: null,
props: null,
group_constrained: null,
shared: null,
},
]);
await DataOperator.handleChannel({
channels: [
{
id: 'kjlw9j1ttnxwig7tnqgebg7dtipno',
create_at: 1600185541285,
update_at: 1604401077256,
delete_at: 0,
team_id: '',
type: 'D',
display_name: '',
name: 'gh781zkzkhh357b4bejephjz5u8daw__9ciscaqbrpd6d8s68k76xb9bte',
header: '(https://mattermost',
purpose: '',
last_post_at: 1617311494451,
total_msg_count: 585,
extra_update_at: 0,
creator_id: '',
scheme_id: null,
props: null,
group_constrained: null,
shared: null,
},
],
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1564,8 +1628,7 @@ describe('*** DataOperator: Handlers tests ***', () => {
team_id: '',
type: 'D',
display_name: '',
name:
'gh781zkzkhh357b4bejephjz5u8daw__9ciscaqbrpd6d8s68k76xb9bte',
name: 'gh781zkzkhh357b4bejephjz5u8daw__9ciscaqbrpd6d8s68k76xb9bte',
header: '(https://mattermost',
purpose: '',
last_post_at: 1617311494451,
@@ -1592,20 +1655,23 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleMyChannelSettings([
{
channel_id: 'c',
notify_props: {
desktop: 'all',
desktop_sound: true,
email: true,
first_name: true,
mention_keys: '',
push: 'mention',
channel: true,
await DataOperator.handleMyChannelSettings({
settings: [
{
channel_id: 'c',
notify_props: {
desktop: 'all',
desktop_sound: true,
email: true,
first_name: true,
mention_keys: '',
push: 'mention',
channel: true,
},
},
},
]);
],
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1638,16 +1704,19 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleChannelInfo([
{
channel_id: 'c',
guest_count: 10,
header: 'channel info header',
member_count: 10,
pinned_post_count: 3,
purpose: 'sample channel ',
},
]);
await DataOperator.handleChannelInfo({
channelInfos: [
{
channel_id: 'c',
guest_count: 10,
header: 'channel info header',
member_count: 10,
pinned_post_count: 3,
purpose: 'sample channel ',
},
],
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({
@@ -1676,16 +1745,19 @@ describe('*** DataOperator: Handlers tests ***', () => {
await createConnection(true);
await DataOperator.handleMyChannel([
{
channel_id: 'c',
last_post_at: 1617311494451,
last_viewed_at: 1617311494451,
mentions_count: 3,
message_count: 10,
roles: 'guest',
},
]);
await DataOperator.handleMyChannel({
myChannels: [
{
channel_id: 'c',
last_post_at: 1617311494451,
last_viewed_at: 1617311494451,
mentions_count: 3,
message_count: 10,
roles: 'guest',
},
],
prepareRecordsOnly: false,
});
expect(spyOnExecuteInDatabase).toHaveBeenCalledTimes(1);
expect(spyOnExecuteInDatabase).toHaveBeenCalledWith({

View File

@@ -5,6 +5,7 @@ import {Database, Q} from '@nozbe/watermelondb';
import Model from '@nozbe/watermelondb/Model';
import {MM_TABLES} from '@constants/database';
import DatabaseManager from '@database/manager';
import {
isRecordAppEqualToRaw,
isRecordChannelEqualToRaw,
@@ -72,47 +73,45 @@ import {
} from '@database/operator/prepareRecords/user';
import {createPostsChain, sanitizePosts} from '@database/operator/utils/post';
import {sanitizeReactions} from '@database/operator/utils/reaction';
import DatabaseManager from '@database/manager';
import CustomEmoji from '@typings/database/custom_emoji';
import {
BatchOperationsArgs,
DatabaseInstance,
HandleChannelArgs,
HandleChannelInfoArgs,
HandleChannelMembershipArgs,
HandleDraftArgs,
HandleEntityRecordsArgs,
HandleFilesArgs,
HandleGroupArgs,
HandleGroupMembershipArgs,
HandleGroupsInChannelArgs,
HandleGroupsInTeamArgs,
HandleIsolatedEntityArgs,
HandleMyChannelArgs,
HandleMyChannelSettingsArgs,
HandleMyTeamArgs,
HandlePostMetadataArgs,
HandlePostsArgs,
HandlePreferencesArgs,
HandleReactionsArgs,
HandleSlashCommandArgs,
HandleTeamArgs,
HandleTeamChannelHistoryArgs,
HandleTeamMembershipArgs,
HandleTeamSearchHistoryArgs,
HandleUsersArgs,
MatchExistingRecord,
PostImage,
PrepareForDatabaseArgs,
PostImage, PrepareForDatabaseArgs,
PrepareRecordsArgs,
ProcessInputsArgs,
RawChannel,
RawChannelInfo,
RawChannelMembership,
RawCustomEmoji,
RawDraft,
RawEmbed,
RawFile,
RawGroup,
RawGroupMembership,
RawGroupsInChannel,
RawGroupsInTeam,
RawMyChannel,
RawMyChannelSettings,
RawMyTeam,
RawPost,
RawPostMetadata,
RawPostsInThread,
RawPreference,
RawReaction,
RawSlashCommand,
RawTeam,
RawTeamChannelHistory,
RawTeamMembership,
RawTeamSearchHistory,
RawUser,
RawValue,
} from '@typings/database/database';
import {IsolatedEntities, OperationType} from '@typings/database/enums';
@@ -181,12 +180,14 @@ class DataOperator {
/**
* handleIsolatedEntity: Handler responsible for the Create/Update operations on the isolated entities as described
* by the IsolatedEntities enum
* @param {HandleIsolatedEntityArgs} entityData
* @param {IsolatedEntities} entityData.tableName
* @param {Records} entityData.values
* @returns {Promise<void>}
* @param {HandleIsolatedEntityArgs} isolatedEntityArgs
* @param {IsolatedEntities} isolatedEntityArgs.tableName
* @param {boolean} isolatedEntityArgs.prepareRecordsOnly
* @param {RawValue} isolatedEntityArgs.values
* @throws DataOperatorException
* @returns {Model[] | boolean}
*/
handleIsolatedEntity = async ({tableName, values}: HandleIsolatedEntityArgs) => {
handleIsolatedEntity = async ({tableName, values, prepareRecordsOnly = true}: HandleIsolatedEntityArgs) => {
let findMatchingRecordBy;
let fieldName;
let operator;
@@ -256,22 +257,30 @@ class DataOperator {
}
if (operator && fieldName && findMatchingRecordBy) {
await this.handleEntityRecords({
findMatchingRecordBy,
const records = await this.handleEntityRecords({
fieldName,
findMatchingRecordBy,
operator,
prepareRecordsOnly,
rawValues,
tableName,
});
return prepareRecordsOnly && records?.length && records;
}
return false;
};
/**
* handleDraft: Handler responsible for the Create/Update operations occurring the Draft entity from the 'Server' schema
* @param {RawDraft[]} drafts
* @returns {Promise<void>}
* @param {HandleDraftArgs} draftsArgs
* @param {RawDraft[]} draftsArgs.drafts
* @param {boolean} draftsArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Draft[] | boolean}
*/
handleDraft = async (drafts: RawDraft[]) => {
handleDraft = async ({drafts, prepareRecordsOnly = true}: HandleDraftArgs) => {
if (!drafts.length) {
throw new DataOperatorException(
'An empty "drafts" array has been passed to the handleReactions method',
@@ -280,24 +289,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: drafts, key: 'channel_id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordDraftEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'channel_id',
findMatchingRecordBy: isRecordDraftEqualToRaw,
operator: prepareDraftRecord,
prepareRecordsOnly,
rawValues,
tableName: DRAFT,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleReactions: Handler responsible for the Create/Update operations occurring on the Reaction entity from the 'Server' schema
* @param {HandleReactionsArgs} handleReactions
* @param {RawReaction[]} handleReactions.reactions
* @param {boolean} handleReactions.prepareRowsOnly
* @param {boolean} handleReactions.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<[] | (Reaction | CustomEmoji)[]>}
* @returns {boolean | (Reaction | CustomEmoji)[]}
*/
handleReactions = async ({reactions, prepareRowsOnly}: HandleReactionsArgs) => {
handleReactions = async ({reactions, prepareRecordsOnly}: HandleReactionsArgs) => {
if (!reactions.length) {
throw new DataOperatorException(
'An empty "reactions" array has been passed to the handleReactions method',
@@ -344,7 +356,7 @@ class DataOperator {
batchRecords = batchRecords.concat(deleteReactions);
if (prepareRowsOnly) {
if (prepareRecordsOnly) {
return batchRecords;
}
@@ -355,7 +367,7 @@ class DataOperator {
});
}
return [];
return false;
};
/**
@@ -460,7 +472,7 @@ class DataOperator {
// calls handler for Reactions
const postReactions = (await this.handleReactions({
reactions,
prepareRowsOnly: true,
prepareRecordsOnly: true,
})) as Reaction[];
batch = batch.concat(postReactions);
@@ -470,7 +482,7 @@ class DataOperator {
// calls handler for Files
const postFiles = (await this.handleFiles({
files,
prepareRowsOnly: true,
prepareRecordsOnly: true,
})) as File[];
batch = batch.concat(postFiles);
@@ -481,7 +493,7 @@ class DataOperator {
const postMetadata = (await this.handlePostMetadata({
images,
embeds,
prepareRowsOnly: true,
prepareRecordsOnly: true,
})) as PostMetadata[];
batch = batch.concat(postMetadata);
@@ -493,7 +505,8 @@ class DataOperator {
// LAST: calls handler for CustomEmojis, PostsInThread, PostsInChannel
if (emojis.length) {
await this.handleIsolatedEntity({tableName: IsolatedEntities.CUSTOM_EMOJI, values: emojis});
await this.handleIsolatedEntity({tableName: IsolatedEntities.CUSTOM_EMOJI, values: emojis, prepareRecordsOnly: false,
});
}
if (postsInThread.length) {
@@ -513,20 +526,19 @@ class DataOperator {
operator: preparePostRecord,
rawValues: postsUnordered,
tableName: POST,
prepareRecordsOnly: false,
});
}
};
// TODO : prepareRowsOnly should be defaulted to true for all handlers
/**
* handleFiles: Handler responsible for the Create/Update operations occurring on the File entity from the 'Server' schema
* @param {HandleFilesArgs} handleFiles
* @param {RawFile[]} handleFiles.files
* @param {boolean} handleFiles.prepareRowsOnly
* @param {boolean} handleFiles.prepareRecordsOnly
* @returns {Promise<File[] | any[]>}
*/
private handleFiles = async ({files, prepareRowsOnly}: HandleFilesArgs) => {
private handleFiles = async ({files, prepareRecordsOnly}: HandleFilesArgs) => {
if (!files.length) {
return [];
}
@@ -540,7 +552,7 @@ class DataOperator {
tableName: FILE,
})) as File[];
if (prepareRowsOnly) {
if (prepareRecordsOnly) {
return postFiles;
}
@@ -556,10 +568,10 @@ class DataOperator {
* @param {HandlePostMetadataArgs} handlePostMetadata
* @param {{embed: RawEmbed[], postId: string}[] | undefined} handlePostMetadata.embeds
* @param {{images: Dictionary<PostImage>, postId: string}[] | undefined} handlePostMetadata.images
* @param {boolean} handlePostMetadata.prepareRowsOnly
* @param {boolean} handlePostMetadata.prepareRecordsOnly
* @returns {Promise<any[] | PostMetadata[]>}
*/
private handlePostMetadata = async ({embeds, images, prepareRowsOnly}: HandlePostMetadataArgs) => {
private handlePostMetadata = async ({embeds, images, prepareRecordsOnly}: HandlePostMetadataArgs) => {
const metadata: RawPostMetadata[] = [];
if (images?.length) {
@@ -598,7 +610,7 @@ class DataOperator {
tableName: POST_METADATA,
})) as PostMetadata[];
if (prepareRowsOnly) {
if (prepareRecordsOnly) {
return postMetas;
}
@@ -704,9 +716,8 @@ class DataOperator {
})) as PostsInChannel[];
const createPostsInChannelRecord = async () => {
const createPostsInChannel = {channel_id: channelId, earliest, latest};
await this.executeInDatabase({
createRaws: [{record: undefined, raw: createPostsInChannel}],
createRaws: [{record: undefined, raw: {channel_id: channelId, earliest, latest}}],
tableName: POSTS_IN_CHANNEL,
recordOperator: preparePostsInChannelRecord,
});
@@ -770,11 +781,13 @@ class DataOperator {
/**
* handleUsers: Handler responsible for the Create/Update operations occurring on the User entity from the 'Server' schema
* @param {RawUser[]} users
* @param {HandleUsersArgs} usersArgs
* @param {RawUser[]} usersArgs.users
* @param {boolean} usersArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<void>}
* @returns {User[] | boolean}
*/
handleUsers = async (users: RawUser[]) => {
handleUsers = async ({users, prepareRecordsOnly = true}: HandleUsersArgs) => {
if (!users.length) {
throw new DataOperatorException(
'An empty "users" array has been passed to the handleUsers method',
@@ -783,22 +796,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: users, key: 'id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordUserEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'id',
findMatchingRecordBy: isRecordUserEqualToRaw,
operator: prepareUserRecord,
rawValues,
tableName: USER,
prepareRecordsOnly,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handlePreferences: Handler responsible for the Create/Update operations occurring on the PREFERENCE entity from the 'Server' schema
* @param {RawPreference[]} preferences
* @param {HandlePreferencesArgs} preferencesArgs
* @param {RawPreference[]} preferencesArgs.preferences
* @param {boolean} preferencesArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {Preference[] | boolean}
*/
handlePreferences = async (preferences: RawPreference[]) => {
handlePreferences = async ({preferences, prepareRecordsOnly = true}: HandlePreferencesArgs) => {
if (!preferences.length) {
throw new DataOperatorException(
'An empty "preferences" array has been passed to the handlePreferences method',
@@ -807,22 +825,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: preferences, key: 'name'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordPreferenceEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'user_id',
findMatchingRecordBy: isRecordPreferenceEqualToRaw,
operator: preparePreferenceRecord,
prepareRecordsOnly,
rawValues,
tableName: PREFERENCE,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleTeamMemberships: Handler responsible for the Create/Update operations occurring on the TEAM_MEMBERSHIP entity from the 'Server' schema
* @param {RawTeamMembership[]} teamMemberships
* @param {HandleTeamMembershipArgs} teamMembershipsArgs
* @param {RawTeamMembership[]} teamMembershipsArgs.teamMemberships
* @param {boolean} teamMembershipsArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {TeamMembership[] | boolean}
*/
handleTeamMemberships = async (teamMemberships: RawTeamMembership[]) => {
handleTeamMemberships = async ({teamMemberships, prepareRecordsOnly = true}: HandleTeamMembershipArgs) => {
if (!teamMemberships.length) {
throw new DataOperatorException(
'An empty "teamMemberships" array has been passed to the handleTeamMemberships method',
@@ -831,22 +854,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: teamMemberships, key: 'team_id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordTeamMembershipEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'user_id',
findMatchingRecordBy: isRecordTeamMembershipEqualToRaw,
operator: prepareTeamMembershipRecord,
rawValues,
tableName: TEAM_MEMBERSHIP,
prepareRecordsOnly,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleGroupMembership: Handler responsible for the Create/Update operations occurring on the GROUP_MEMBERSHIP entity from the 'Server' schema
* @param {RawGroupMembership[]} groupMemberships
* @param {HandleGroupMembershipArgs} groupMembershipsArgs
* @param {RawGroupMembership[]} groupMembershipsArgs.groupMemberships
* @param {boolean} groupMembershipsArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<void>}
* @returns {GroupMembership[] | boolean}
*/
handleGroupMembership = async (groupMemberships: RawGroupMembership[]) => {
handleGroupMembership = async ({groupMemberships, prepareRecordsOnly = true}: HandleGroupMembershipArgs) => {
if (!groupMemberships.length) {
throw new DataOperatorException(
'An empty "groupMemberships" array has been passed to the handleGroupMembership method',
@@ -855,22 +883,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: groupMemberships, key: 'group_id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordGroupMembershipEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'user_id',
findMatchingRecordBy: isRecordGroupMembershipEqualToRaw,
operator: prepareGroupMembershipRecord,
prepareRecordsOnly,
rawValues,
tableName: GROUP_MEMBERSHIP,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleChannelMembership: Handler responsible for the Create/Update operations occurring on the CHANNEL_MEMBERSHIP entity from the 'Server' schema
* @param {RawChannelMembership[]} channelMemberships
* @param {HandleChannelMembershipArgs} channelMembershipsArgs
* @param {RawChannelMembership[]} channelMembershipsArgs.channelMemberships
* @param {boolean} channelMembershipsArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {ChannelMembership[] | boolean}
*/
handleChannelMembership = async (channelMemberships: RawChannelMembership[]) => {
handleChannelMembership = async ({channelMemberships, prepareRecordsOnly = true}: HandleChannelMembershipArgs) => {
if (!channelMemberships.length) {
throw new DataOperatorException(
'An empty "channelMemberships" array has been passed to the handleChannelMembership method',
@@ -879,22 +912,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: channelMemberships, key: 'channel_id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordChannelMembershipEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'user_id',
findMatchingRecordBy: isRecordChannelMembershipEqualToRaw,
operator: prepareChannelMembershipRecord,
prepareRecordsOnly,
rawValues,
tableName: CHANNEL_MEMBERSHIP,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleGroup: Handler responsible for the Create/Update operations occurring on the GROUP entity from the 'Server' schema
* @param {RawGroup[]} groups
* @param {HandleGroupArgs} groupsArgs
* @param {RawGroup[]} groupsArgs.groups
* @param {boolean} groupsArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {Group[] | boolean}
*/
handleGroup = async (groups: RawGroup[]) => {
handleGroup = async ({groups, prepareRecordsOnly = true}: HandleGroupArgs) => {
if (!groups.length) {
throw new DataOperatorException(
'An empty "groups" array has been passed to the handleGroup method',
@@ -903,22 +941,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: groups, key: 'name'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordGroupEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'name',
findMatchingRecordBy: isRecordGroupEqualToRaw,
operator: prepareGroupRecord,
prepareRecordsOnly,
rawValues,
tableName: GROUP,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleGroupsInTeam: Handler responsible for the Create/Update operations occurring on the GROUPS_IN_TEAM entity from the 'Server' schema
* @param {RawGroupsInTeam[]} groupsInTeams
* @param {HandleGroupsInTeamArgs} groupsInTeamsArgs
* @param {RawGroupsInTeam[]} groupsInTeamsArgs.groupsInTeams
* @param {boolean} groupsInTeamsArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {GroupsInTeam[] | boolean}
*/
handleGroupsInTeam = async (groupsInTeams: RawGroupsInTeam[]) => {
handleGroupsInTeam = async ({groupsInTeams, prepareRecordsOnly = true} : HandleGroupsInTeamArgs) => {
if (!groupsInTeams.length) {
throw new DataOperatorException(
'An empty "groups" array has been passed to the handleGroupsInTeam method',
@@ -927,22 +970,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: groupsInTeams, key: 'group_id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordGroupsInTeamEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'group_id',
findMatchingRecordBy: isRecordGroupsInTeamEqualToRaw,
operator: prepareGroupsInTeamRecord,
prepareRecordsOnly,
rawValues,
tableName: GROUPS_IN_TEAM,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleGroupsInChannel: Handler responsible for the Create/Update operations occurring on the GROUPS_IN_CHANNEL entity from the 'Server' schema
* @param {RawGroupsInChannel[]} groupsInChannels
* @param {HandleGroupsInChannelArgs} groupsInChannelsArgs
* @param {RawGroupsInChannel[]} groupsInChannelsArgs.groupsInChannels
* @param {boolean} groupsInChannelsArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {GroupsInChannel[] | boolean}
*/
handleGroupsInChannel = async (groupsInChannels: RawGroupsInChannel[]) => {
handleGroupsInChannel = async ({groupsInChannels, prepareRecordsOnly = true}: HandleGroupsInChannelArgs) => {
if (!groupsInChannels.length) {
throw new DataOperatorException(
'An empty "groups" array has been passed to the handleGroupsInTeam method',
@@ -951,22 +999,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: groupsInChannels, key: 'channel_id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordGroupsInChannelEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'group_id',
findMatchingRecordBy: isRecordGroupsInChannelEqualToRaw,
operator: prepareGroupsInChannelRecord,
prepareRecordsOnly,
rawValues,
tableName: GROUPS_IN_CHANNEL,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleTeam: Handler responsible for the Create/Update operations occurring on the TEAM entity from the 'Server' schema
* @param {RawTeam[]} teams
* @param {HandleTeamArgs} teamsArgs
* @param {RawTeam[]} teamsArgs.teams
* @param {boolean} teamsArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {Team[] | boolean}
*/
handleTeam = async (teams: RawTeam[]) => {
handleTeam = async ({teams, prepareRecordsOnly = true}: HandleTeamArgs) => {
if (!teams.length) {
throw new DataOperatorException(
'An empty "teams" array has been passed to the handleTeam method',
@@ -975,22 +1028,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: teams, key: 'id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordTeamEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'id',
findMatchingRecordBy: isRecordTeamEqualToRaw,
operator: prepareTeamRecord,
prepareRecordsOnly,
rawValues,
tableName: TEAM,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleTeamChannelHistory: Handler responsible for the Create/Update operations occurring on the TEAM_CHANNEL_HISTORY entity from the 'Server' schema
* @param {RawTeamChannelHistory[]} teamChannelHistories
* @param {HandleTeamChannelHistoryArgs} teamChannelHistoriesArgs
* @param {RawTeamChannelHistory[]} teamChannelHistoriesArgs.teamChannelHistories
* @param {boolean} teamChannelHistoriesArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {TeamChannelHistory[]| boolean}
*/
handleTeamChannelHistory = async (teamChannelHistories: RawTeamChannelHistory[]) => {
handleTeamChannelHistory = async ({teamChannelHistories, prepareRecordsOnly = true}: HandleTeamChannelHistoryArgs) => {
if (!teamChannelHistories.length) {
throw new DataOperatorException(
'An empty "teamChannelHistories" array has been passed to the handleTeamChannelHistory method',
@@ -999,22 +1057,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: teamChannelHistories, key: 'team_id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordTeamChannelHistoryEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'team_id',
findMatchingRecordBy: isRecordTeamChannelHistoryEqualToRaw,
operator: prepareTeamChannelHistoryRecord,
prepareRecordsOnly,
rawValues,
tableName: TEAM_CHANNEL_HISTORY,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleTeamSearchHistory: Handler responsible for the Create/Update operations occurring on the TEAM_SEARCH_HISTORY entity from the 'Server' schema
* @param {RawTeamSearchHistory[]} teamSearchHistories
* @param {HandleTeamSearchHistoryArgs} teamSearchHistoriesArgs
* @param {RawTeamSearchHistory[]} teamSearchHistoriesArgs.teamSearchHistories
* @param {boolean} teamSearchHistoriesArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {TeamSearchHistory[]| boolean}
*/
handleTeamSearchHistory = async (teamSearchHistories: RawTeamSearchHistory[]) => {
handleTeamSearchHistory = async ({teamSearchHistories, prepareRecordsOnly = true}: HandleTeamSearchHistoryArgs) => {
if (!teamSearchHistories.length) {
throw new DataOperatorException(
'An empty "teamSearchHistories" array has been passed to the handleTeamSearchHistory method',
@@ -1023,22 +1086,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: teamSearchHistories, key: 'term'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordTeamSearchHistoryEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'team_id',
findMatchingRecordBy: isRecordTeamSearchHistoryEqualToRaw,
operator: prepareTeamSearchHistoryRecord,
prepareRecordsOnly,
rawValues,
tableName: TEAM_SEARCH_HISTORY,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleSlashCommand: Handler responsible for the Create/Update operations occurring on the SLASH_COMMAND entity from the 'Server' schema
* @param {RawSlashCommand[]} slashCommands
* @param {HandleSlashCommandArgs} slashCommandsArgs
* @param {RawSlashCommand[]} slashCommandsArgs.slashCommands
* @param {boolean} slashCommandsArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {SlashCommand[]| boolean}
*/
handleSlashCommand = async (slashCommands: RawSlashCommand[]) => {
handleSlashCommand = async ({slashCommands, prepareRecordsOnly = true} : HandleSlashCommandArgs) => {
if (!slashCommands.length) {
throw new DataOperatorException(
'An empty "slashCommands" array has been passed to the handleSlashCommand method',
@@ -1047,22 +1115,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: slashCommands, key: 'id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordSlashCommandEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'id',
findMatchingRecordBy: isRecordSlashCommandEqualToRaw,
operator: prepareSlashCommandRecord,
prepareRecordsOnly,
rawValues,
tableName: SLASH_COMMAND,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleMyTeam: Handler responsible for the Create/Update operations occurring on the MY_TEAM entity from the 'Server' schema
* @param {RawMyTeam[]} myTeams
* @param {HandleMyTeamArgs} myTeamsArgs
* @param {RawMyTeam[]} myTeamsArgs.myTeams
* @param {boolean} myTeamsArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {MyTeam[]| boolean}
*/
handleMyTeam = async (myTeams: RawMyTeam[]) => {
handleMyTeam = async ({myTeams, prepareRecordsOnly = true}: HandleMyTeamArgs) => {
if (!myTeams.length) {
throw new DataOperatorException(
'An empty "myTeams" array has been passed to the handleSlashCommand method',
@@ -1071,22 +1144,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: myTeams, key: 'team_id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordMyTeamEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'team_id',
findMatchingRecordBy: isRecordMyTeamEqualToRaw,
operator: prepareMyTeamRecord,
prepareRecordsOnly,
rawValues,
tableName: MY_TEAM,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleChannel: Handler responsible for the Create/Update operations occurring on the CHANNEL entity from the 'Server' schema
* @param {RawChannel[]} channels
* @param {HandleChannelArgs} channelsArgs
* @param {RawChannel[]} channelsArgs.channels
* @param {boolean} channelsArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {Channel[]|boolean}
*/
handleChannel = async (channels: RawChannel[]) => {
handleChannel = async ({channels, prepareRecordsOnly = true}: HandleChannelArgs) => {
if (!channels.length) {
throw new DataOperatorException(
'An empty "channels" array has been passed to the handleChannel method',
@@ -1095,22 +1173,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: channels, key: 'id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordChannelEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'id',
findMatchingRecordBy: isRecordChannelEqualToRaw,
operator: prepareChannelRecord,
prepareRecordsOnly,
rawValues,
tableName: CHANNEL,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleMyChannelSettings: Handler responsible for the Create/Update operations occurring on the MY_CHANNEL_SETTINGS entity from the 'Server' schema
* @param {RawMyChannelSettings[]} settings
* @param {HandleMyChannelSettingsArgs} settingsArgs
* @param {RawMyChannelSettings[]} settingsArgs.settings
* @param {boolean} settingsArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {MyChannelSettings[]| boolean}
*/
handleMyChannelSettings = async (settings: RawMyChannelSettings[]) => {
handleMyChannelSettings = async ({settings, prepareRecordsOnly = true}: HandleMyChannelSettingsArgs) => {
if (!settings.length) {
throw new DataOperatorException(
'An empty "settings" array has been passed to the handleMyChannelSettings method',
@@ -1119,22 +1202,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: settings, key: 'channel_id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordMyChannelSettingsEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'channel_id',
findMatchingRecordBy: isRecordMyChannelSettingsEqualToRaw,
operator: prepareMyChannelSettingsRecord,
prepareRecordsOnly,
rawValues,
tableName: MY_CHANNEL_SETTINGS,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleChannelInfo: Handler responsible for the Create/Update operations occurring on the CHANNEL_INFO entity from the 'Server' schema
* @param {RawChannelInfo[]} channelInfos
* @param {HandleChannelInfoArgs} channelInfosArgs
* @param {RawChannelInfo[]} channelInfosArgs.channelInfos
* @param {boolean} channelInfosArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {ChannelInfo[]| boolean}
*/
handleChannelInfo = async (channelInfos: RawChannelInfo[]) => {
handleChannelInfo = async ({channelInfos, prepareRecordsOnly = true}: HandleChannelInfoArgs) => {
if (!channelInfos.length) {
throw new DataOperatorException(
'An empty "channelInfos" array has been passed to the handleMyChannelSettings method',
@@ -1143,22 +1231,27 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: channelInfos, key: 'channel_id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordChannelInfoEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'channel_id',
findMatchingRecordBy: isRecordChannelInfoEqualToRaw,
operator: prepareChannelInfoRecord,
prepareRecordsOnly,
rawValues,
tableName: CHANNEL_INFO,
});
return prepareRecordsOnly && records?.length && records;
};
/**
* handleMyChannel: Handler responsible for the Create/Update operations occurring on the MY_CHANNEL entity from the 'Server' schema
* @param {RawMyChannel[]} myChannels
* @param {HandleMyChannelArgs} myChannelsArgs
* @param {RawMyChannel[]} myChannelsArgs.myChannels
* @param {boolean} myChannelsArgs.prepareRecordsOnly
* @throws DataOperatorException
* @returns {Promise<null|void>}
* @returns {MyChannel[]| boolean}
*/
handleMyChannel = async (myChannels: RawMyChannel[]) => {
handleMyChannel = async ({myChannels, prepareRecordsOnly = true}: HandleMyChannelArgs) => {
if (!myChannels.length) {
throw new DataOperatorException(
'An empty "myChannels" array has been passed to the handleMyChannel method',
@@ -1167,13 +1260,16 @@ class DataOperator {
const rawValues = getUniqueRawsBy({raws: myChannels, key: 'channel_id'});
await this.handleEntityRecords({
findMatchingRecordBy: isRecordMyChannelEqualToRaw,
const records = await this.handleEntityRecords({
fieldName: 'channel_id',
findMatchingRecordBy: isRecordMyChannelEqualToRaw,
operator: prepareMyChannelRecord,
prepareRecordsOnly,
rawValues,
tableName: MY_CHANNEL,
});
return prepareRecordsOnly && records?.length && records;
};
/**
@@ -1184,9 +1280,9 @@ class DataOperator {
* @param {(DataFactoryArgs) => Promise<Model>} handleEntityArgs.operator
* @param {RawValue[]} handleEntityArgs.rawValues
* @param {string} handleEntityArgs.tableName
* @returns {Promise<null | void>}
* @returns {Promise<null | Model[]>}
*/
private handleEntityRecords = async ({findMatchingRecordBy, fieldName, operator, rawValues, tableName}: HandleEntityRecordsArgs) => {
private handleEntityRecords = async ({findMatchingRecordBy, fieldName, operator, rawValues, tableName, prepareRecordsOnly = true}: HandleEntityRecordsArgs) => {
if (!rawValues.length) {
return null;
}
@@ -1197,15 +1293,25 @@ class DataOperator {
findMatchingRecordBy,
fieldName,
});
const database = await this.getDatabase(tableName);
const records = await this.executeInDatabase({
recordOperator: operator,
const models = (await this.prepareRecords({
database,
tableName,
createRaws,
updateRaws,
});
recordOperator: operator,
})) as Model[];
return records;
if (prepareRecordsOnly) {
return models;
}
if (models?.length > 0) {
await this.batchOperations({database, models});
}
return null;
};
/**
@@ -1283,7 +1389,7 @@ class DataOperator {
* @throws {DataOperatorException}
* @returns {Promise<void>}
*/
private batchOperations = async ({database, models}: BatchOperationsArgs) => {
batchOperations = async ({database, models}: BatchOperationsArgs) => {
try {
if (models.length > 0) {
await database.action(async () => {
@@ -1336,6 +1442,7 @@ class DataOperator {
preparedRecords = preparedRecords.concat(recordPromises);
}
const results = ((await Promise.all(preparedRecords)) as unknown) as Model[];
return results;
};

View File

@@ -96,7 +96,7 @@ describe('DataOperator: Utils tests', () => {
delete_at: 0,
},
],
prepareRowsOnly: true,
prepareRecordsOnly: true,
});
// Jest in not using the same database instance amongst the Singletons; hence, we are creating the reaction record here

View File

@@ -357,16 +357,16 @@ export type RawTeam = {
};
export type RawTeamChannelHistory = {
team_id: string;
channel_ids: string[]
}
team_id: string;
channel_ids: string[];
};
export type RawTeamSearchHistory = {
created_at: number;
display_term: string;
term: string;
team_id: string;
}
};
export type RawSlashCommand = {
id: string;
@@ -419,9 +419,9 @@ export type RawChannel = {
};
export type RawMyChannelSettings = {
notify_props: NotifyProps,
notify_props: NotifyProps;
channel_id: string;
}
};
export type RawChannelInfo = {
channel_id: string;
@@ -430,7 +430,7 @@ export type RawChannelInfo = {
member_count: number;
pinned_post_count: number;
purpose: string;
}
};
export type RawMyChannel = {
channel_id: string;
@@ -439,7 +439,7 @@ export type RawMyChannel = {
mentions_count: number;
message_count: number;
roles: string;
}
};
export type RawValue =
| RawApp
@@ -500,6 +500,7 @@ export type BatchOperationsArgs = { database: Database; models: Model[] };
export type HandleIsolatedEntityArgs = {
tableName: IsolatedEntities;
values: RawValue[];
prepareRecordsOnly: boolean;
};
export type Models = Class<Model>[];
@@ -517,19 +518,19 @@ export type ActiveServerDatabaseArgs = {
};
export type HandleReactionsArgs = {
prepareRowsOnly: boolean;
prepareRecordsOnly: boolean;
reactions: RawReaction[];
};
export type HandleFilesArgs = {
files: RawFile[];
prepareRowsOnly: boolean;
prepareRecordsOnly: boolean;
};
export type HandlePostMetadataArgs = {
embeds?: { embed: RawEmbed[]; postId: string }[];
images?: { images: Dictionary<PostImage>; postId: string }[];
prepareRowsOnly: boolean;
prepareRecordsOnly: boolean;
};
export type HandlePostsArgs = {
@@ -580,6 +581,7 @@ export type HandleEntityRecordsArgs = {
operator: (DataFactoryArgs) => Promise<Model>;
rawValues: RawValue[];
tableName: string;
prepareRecordsOnly: boolean;
};
export type DatabaseInstances = {
@@ -596,3 +598,87 @@ export type RecordPair = {
record?: Model;
raw: RawValue;
};
export type HandleMyChannelArgs = {
myChannels: RawMyChannel[];
prepareRecordsOnly: boolean;
};
export type HandleChannelInfoArgs = {
channelInfos: RawChannelInfo[];
prepareRecordsOnly: boolean;
};
export type HandleMyChannelSettingsArgs = {
settings: RawMyChannelSettings[];
prepareRecordsOnly: boolean;
};
export type HandleChannelArgs = {
channels: RawChannel[];
prepareRecordsOnly: boolean;
};
export type HandleMyTeamArgs = {
myTeams: RawMyTeam[];
prepareRecordsOnly: boolean;
};
export type HandleSlashCommandArgs = {
slashCommands: RawSlashCommand[];
prepareRecordsOnly: boolean;
};
export type HandleTeamSearchHistoryArgs = {
teamSearchHistories: RawTeamSearchHistory[];
prepareRecordsOnly: boolean;
};
export type HandleTeamChannelHistoryArgs = {
teamChannelHistories: RawTeamChannelHistory[];
prepareRecordsOnly: boolean;
};
export type HandleTeamArgs = { teams: RawTeam[]; prepareRecordsOnly: boolean };
export type HandleGroupsInChannelArgs = {
groupsInChannels: RawGroupsInChannel[];
prepareRecordsOnly: boolean;
};
export type HandleGroupsInTeamArgs = {
groupsInTeams: RawGroupsInTeam[];
prepareRecordsOnly: boolean;
};
export type HandleGroupArgs = {
groups: RawGroup[];
prepareRecordsOnly: boolean;
};
export type HandleChannelMembershipArgs = {
channelMemberships: RawChannelMembership[];
prepareRecordsOnly: boolean;
};
export type HandleGroupMembershipArgs = {
groupMemberships: RawGroupMembership[];
prepareRecordsOnly: boolean;
};
export type HandleTeamMembershipArgs = {
teamMemberships: RawTeamMembership[];
prepareRecordsOnly: boolean;
};
export type HandlePreferencesArgs = {
preferences: RawPreference[];
prepareRecordsOnly: boolean;
};
export type HandleUsersArgs = { users: RawUser[]; prepareRecordsOnly: boolean };
export type HandleDraftArgs = {
drafts: RawDraft[];
prepareRecordsOnly: boolean;
};