34 lines
1.5 KiB
JavaScript
34 lines
1.5 KiB
JavaScript
import { GET_BY_COMPANYUSER_AND_ID as MSSQL_GET } from '../../providers/mssql/queries/modelconversation.js';
|
|
import { GET_BY_COMPANYUSER_AND_ID as SQLITE_GET } from '../../providers/sqlite3/queries/modelconversation.js';
|
|
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
|
import makeRunner from '../runner.js';
|
|
import shape from './shape.js';
|
|
|
|
export default function get(provider) {
|
|
const access = resolveSelectedCompanyAccess(provider);
|
|
const sql = provider?.dialect === 'sqlite3' ? SQLITE_GET : MSSQL_GET;
|
|
|
|
/**
|
|
* Gets a single model conversation by ID for the user in the selected company.
|
|
*
|
|
* @param {number} googleAuthId - The id of the Google auth user.
|
|
* @param {number} conversationId - The conversation id.
|
|
* @param {object} [tx] - Optional transaction to run within.
|
|
* @returns {Promise<{id: number, companyuser_id: number, provider: string, model: string, created_at: string, updated_at: string, state: string, started_by: number|null}>}
|
|
* @throws {Error} 'Conversation not found'.
|
|
*/
|
|
return async (googleAuthId, conversationId, tx) => {
|
|
const id = Number(conversationId);
|
|
if (!Number.isInteger(id) || id <= 0) {
|
|
throw new Error('Conversation not found');
|
|
}
|
|
|
|
const runner = makeRunner(provider, tx);
|
|
const { companyuserId } = await access(googleAuthId, runner);
|
|
|
|
const result = await runner.query(sql, { id, companyuserId });
|
|
if (!result.rows[0]) throw new Error('Conversation not found');
|
|
return shape(result.rows[0]);
|
|
};
|
|
}
|