mattermost-mobile/app/queries/servers/categories.ts
Shaz Amjad 9f5f73b264
Gekidou - Sidebar - Adds actions, queries, db models and rest client (#5953)
* Adds actions, queries, db models and rest client

* move fetching categories inside fetchMyChannelsForTeam

* code cleanup

* deconstruct categoriesWithOrder

* DMs fetching for changing teams

* Fix bad merge and address feedback

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2022-02-11 18:40:44 -03:00

103 lines
3.4 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Database, Q} from '@nozbe/watermelondb';
import {MM_TABLES} from '@constants/database';
import type ServerDataOperator from '@database/operator/server_data_operator';
import type CategoryModel from '@typings/database/models/servers/category';
const {SERVER: {CATEGORY}} = MM_TABLES;
export const queryCategoryById = async (database: Database, categoryId: string) => {
try {
const record = (await database.collections.get<CategoryModel>(CATEGORY).find(categoryId));
return record;
} catch {
return undefined;
}
};
export const queryCategoriesById = async (database: Database, categoryIds: string[]): Promise<CategoryModel[]> => {
try {
const records = (await database.get<CategoryModel>(CATEGORY).query(Q.where('id', Q.oneOf(categoryIds))).fetch());
return records;
} catch {
return Promise.resolve([] as CategoryModel[]);
}
};
export const queryCategoriesByType = async (database: Database, type: CategoryType): Promise<CategoryModel[]> => {
try {
const records = (await database.get<CategoryModel>(CATEGORY).query(Q.where('type', type)).fetch());
return records;
} catch {
return Promise.resolve([] as CategoryModel[]);
}
};
export const queryCategoriesByTeamId = async (database: Database, teamId: string): Promise<CategoryModel[]> => {
try {
const records = (await database.get<CategoryModel>(CATEGORY).query(Q.where('team_id', teamId)).fetch());
return records;
} catch {
return Promise.resolve([] as CategoryModel[]);
}
};
export const queryCategoriesByTypeTeamId = async (database: Database, type: CategoryType, teamId: string): Promise<CategoryModel[]> => {
try {
const records = await database.get<CategoryModel>(CATEGORY).query(
Q.where('team_id', teamId),
Q.where('type', type),
).fetch();
return records;
} catch {
return Promise.resolve([] as CategoryModel[]);
}
};
export const queryAllCategories = async (database: Database): Promise<CategoryModel[]> => {
try {
const records = await database.get<CategoryModel>(CATEGORY).query().fetch();
return records;
} catch {
return Promise.resolve([] as CategoryModel[]);
}
};
export const prepareCategories = (operator: ServerDataOperator, categories: CategoryWithChannels[]) => {
try {
const categoryRecords = operator.handleCategories({categories, prepareRecordsOnly: true});
return [categoryRecords];
} catch {
return undefined;
}
};
export const prepareCategoryChannels = (
operator: ServerDataOperator,
categories: CategoryWithChannels[],
) => {
try {
const categoryChannels: CategoryChannel[] = [];
categories.forEach((category) => {
category.channel_ids.forEach((channelId, index) => {
categoryChannels.push({
id: `${category.team_id}_${channelId}`,
category_id: category.id,
channel_id: channelId,
sort_order: index,
});
});
});
const categoryChannelRecords = operator.handleCategoryChannels({categoryChannels, prepareRecordsOnly: true});
return [categoryChannelRecords];
} catch (e) {
return undefined;
}
};