* Add the channel options to get into playbooks (#8750) * Add the channel options to get into playbooks * Use playbook run id instead of playbook id * i18n * Fix issues and move playbooks to the products folder * Address some tests * Fix test * Address design issues * Add requested comment --------- Co-authored-by: Elias Nahum <nahumhbl@gmail.com> * Playbooks database (#8802) * Add lastPlaybookFetchAt to channel table (#8916) * Add lastPlaybookFetchAt to channel table * Add missing commit * Use my_channel table instead * Fix test * Address feedback * First implementation of playbooks API (#8897) * First implementation of playbooks API * Add version check * Address feedback * Fix test * Add last fetch at usage and other improvements * Simplify test * Add sort_order, update_at and previousReminder columns (#8927) * Add sort_order, update_at and previousReminder columns * Remove order from the schema * Fix tests * Add tests * Add websockets for playbooks (#8947) * Add websocket events for playbooks * Fix typo * Add playbook run list (#8761) * Add the channel options to get into playbooks * Use playbook run id instead of playbook id * i18n * Fix issues and move playbooks to the products folder * Address some tests * Fix test * Add playbook run list * Add missing texts * Add back button support and item size to flash list * Address design issues * Add requested comment * Standardize tag and use it in the card * Fix merge * Add API related functionality --------- Co-authored-by: Elias Nahum <nahumhbl@gmail.com> * Add playbooks run details (#8872) * Add the channel options to get into playbooks * Use playbook run id instead of playbook id * i18n * Fix issues and move playbooks to the products folder * Address some tests * Fix test * Add playbook run list * Add missing texts * Add back button support and item size to flash list * Address design issues * Add requested comment * Standardize tag and use it in the card * Add playbooks run details * Fix merge * Add API related functionality * Add API related changes * Order fixes * Several fixes * Add error state on playbook run * i18n-extract * Fix tests * Fix test * Several fixes * Fixes and add missing UI elements * i18n-extract * Fix tests * Remove files from bad merge --------- Co-authored-by: Elias Nahum <nahumhbl@gmail.com> * Add missing tests for playbooks (#8976) * Add the channel options to get into playbooks * Use playbook run id instead of playbook id * i18n * Fix issues and move playbooks to the products folder * Address some tests * Fix test * Add playbook run list * Add missing texts * Add back button support and item size to flash list * Address design issues * Add requested comment * Standardize tag and use it in the card * Add playbooks run details * Fix merge * Add API related functionality * Add API related changes * Order fixes * Several fixes * Add error state on playbook run * i18n-extract * Fix tests * Fix test * Several fixes * Fixes and add missing UI elements * i18n-extract * Fix tests * Remove files from bad merge * Add tests * Fix typo * Add missing strings * Fix tests and skip some * Fix test * Fix typo --------- Co-authored-by: Elias Nahum <nahumhbl@gmail.com> * Address feedback * Address feedback and fix tests * Address comments and fix tests * Address feedback * Address plugin changes and fix bugs --------- Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
144 lines
5.6 KiB
TypeScript
144 lines
5.6 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {Q, type Model} from '@nozbe/watermelondb';
|
|
|
|
import {MM_TABLES} from '@constants/database';
|
|
|
|
import type {AssociationInfo, HasManyAssociation} from '@nozbe/watermelondb/Model';
|
|
import type {IdenticalRecordArgs, RangeOfValueArgs, RecordPair, RetrieveRecordsArgs} from '@typings/database/database';
|
|
|
|
const {CHANNEL, POST, TEAM, USER} = MM_TABLES.SERVER;
|
|
|
|
/**
|
|
* getValidRecordsForUpdate: Database Operations on some tables are expensive. As such, we would like to operate if and only if we are
|
|
* 100% sure that the records are actually different from what we already have in the database.
|
|
* @param {IdenticalRecordArgs} identicalRecord
|
|
* @param {string} identicalRecord.tableName
|
|
* @param {RecordValue} identicalRecord.newValue
|
|
* @param {Model} identicalRecord.existingRecord
|
|
* @returns {boolean}
|
|
*/
|
|
export const getValidRecordsForUpdate = <T extends Model, R extends RawValue>({tableName, newValue, existingRecord}: IdenticalRecordArgs<T, R>) => {
|
|
const guardTables = [CHANNEL, POST, TEAM, USER];
|
|
if (guardTables.includes(tableName)) {
|
|
const newValueUpdateAt = ('update_at' in newValue) ? newValue.update_at : 0;
|
|
const existingRecordUpdateAt = ('updateAt' in existingRecord) ? existingRecord.updateAt : 0;
|
|
const shouldUpdate = newValueUpdateAt === existingRecordUpdateAt;
|
|
|
|
if (shouldUpdate) {
|
|
return {
|
|
record: existingRecord,
|
|
raw: newValue,
|
|
};
|
|
}
|
|
}
|
|
|
|
return {
|
|
record: existingRecord,
|
|
raw: newValue,
|
|
};
|
|
};
|
|
|
|
/**
|
|
* This method extracts one particular field 'fieldName' from the raw values and returns them as a string array
|
|
* @param {RangeOfValueArgs} range
|
|
* @param {string} range.fieldName
|
|
* @param {RawValue[]} range.raws
|
|
* @returns {string[]}
|
|
*/
|
|
export const getRangeOfValues = ({fieldName, raws}: RangeOfValueArgs) => {
|
|
return raws.reduce((oneOfs, current: RawValue) => {
|
|
const key = fieldName as keyof typeof current;
|
|
const value: string = current[key] as string;
|
|
if (value) {
|
|
oneOfs.push(value);
|
|
}
|
|
return oneOfs;
|
|
}, [] as string[]);
|
|
};
|
|
|
|
/**
|
|
* getRawRecordPairs: Utility method that maps over the raws array to create an array of RecordPair
|
|
* @param {any[]} raws
|
|
* @returns {{record: undefined, raw: any}[]}
|
|
*/
|
|
export const getRawRecordPairs = <T extends Model, R extends RawValue>(raws: R[]): Array<RecordPair<T, R>> => {
|
|
return raws.map((raw) => {
|
|
return {raw, record: undefined};
|
|
});
|
|
};
|
|
|
|
/**
|
|
* getUniqueRawsBy: We have to ensure that we are not updating the same record twice in the same operation.
|
|
* Hence, thought it might not occur, prevention is better than cure. This function removes duplicates from the 'raws' array.
|
|
* @param {RawValue[]} raws
|
|
* @param {string} key
|
|
*/
|
|
export const getUniqueRawsBy = <T extends RawValue>({raws, key}: { raws: T[]; key: string}) => {
|
|
return [...new Map(raws.map((item) => {
|
|
const curItemKey = item[key as keyof typeof item];
|
|
return [curItemKey, item];
|
|
})).values()];
|
|
};
|
|
|
|
/**
|
|
* retrieveRecords: Retrieves records from the database
|
|
* @param {RetrieveRecordsArgs} records
|
|
* @param {Database} records.database
|
|
* @param {string} records.tableName
|
|
* @param {Clause} records.condition
|
|
* @returns {Promise<Model[]>}
|
|
*/
|
|
export const retrieveRecords = <T extends Model>({database, tableName, condition}: RetrieveRecordsArgs) => {
|
|
return database.collections.get<T>(tableName).query(condition).fetch();
|
|
};
|
|
|
|
export function isHasManyAssociation(associated: AssociationInfo) {
|
|
return associated.type === 'has_many' && 'foreignKey' in associated;
|
|
}
|
|
|
|
/**
|
|
* prepareDestroyPermanentlyAssociatedRecords: prepares children associated records
|
|
* for permanent deletion
|
|
* @param {Model[]} records
|
|
* @returns {Promise<Model[]>}
|
|
*/
|
|
export const prepareDestroyPermanentlyChildrenAssociatedRecords = async (records: Model[]): Promise<Model[]> => {
|
|
const associationPromises: Array<Promise<Model[]>> = [];
|
|
|
|
const recordsByTable = records.reduce((acc, record) => {
|
|
const tableName = (record.constructor as typeof Model).table;
|
|
if (!acc[tableName]) {
|
|
acc[tableName] = [];
|
|
}
|
|
acc[tableName].push(record);
|
|
return acc;
|
|
}, {} as Record<string, Model[]>);
|
|
|
|
for (const groupedRecords of Object.values(recordsByTable)) {
|
|
const associations = (groupedRecords[0].constructor as typeof Model).associations;
|
|
|
|
const promises = Object.entries(associations).
|
|
filter(([, associated]) => isHasManyAssociation(associated)).
|
|
map(async ([associationName, associated]) => {
|
|
const preparedRecords: Model[] = [];
|
|
const child = associated as HasManyAssociation; // this is a guard as we know that we are in a has_many association
|
|
const relatedRecords = await retrieveRecords({
|
|
database: groupedRecords[0].database,
|
|
tableName: associationName,
|
|
condition: Q.where(child.foreignKey, Q.oneOf(groupedRecords.map((r) => r.id))),
|
|
});
|
|
|
|
const childPreparedRecords = await prepareDestroyPermanentlyChildrenAssociatedRecords(relatedRecords);
|
|
preparedRecords.push(...childPreparedRecords);
|
|
preparedRecords.push(...relatedRecords.map((relatedRecord) => relatedRecord.prepareDestroyPermanently()));
|
|
return preparedRecords;
|
|
});
|
|
|
|
associationPromises.push(...promises);
|
|
}
|
|
|
|
const results = await Promise.all(associationPromises);
|
|
return results.flat();
|
|
};
|