fix(critical): Create revision on document creation
Documents created via MCP were not visible in Outline interface. Outline requires an entry in the revisions table to display documents. Now uses transaction to insert into both documents and revisions tables. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -236,44 +236,73 @@ const createDocument: BaseTool<CreateDocumentArgs> = {
|
||||
);
|
||||
const teamId = teamResult.rows[0]?.teamId;
|
||||
|
||||
const query = `
|
||||
INSERT INTO documents (
|
||||
id, "urlId", title, text, "collectionId", "teamId", "parentDocumentId", "createdById",
|
||||
"lastModifiedById", template, "publishedAt", "createdAt", "updatedAt", version,
|
||||
"isWelcome", "fullWidth", "insightsEnabled"
|
||||
)
|
||||
VALUES (
|
||||
gen_random_uuid(),
|
||||
substring(replace(gen_random_uuid()::text, '-', '') from 1 for 21),
|
||||
$1, $2, $3, $4, $5, $6, $7, $8, $9, NOW(), NOW(), 1, false, false, false
|
||||
)
|
||||
RETURNING id, "urlId", title, "collectionId", "publishedAt", "createdAt"
|
||||
`;
|
||||
// Use transaction to ensure both document and revision are created
|
||||
await pgClient.query('BEGIN');
|
||||
|
||||
const params = [
|
||||
title,
|
||||
text,
|
||||
args.collection_id,
|
||||
teamId,
|
||||
args.parent_document_id || null,
|
||||
userId,
|
||||
userId,
|
||||
args.template || false,
|
||||
publishedAt
|
||||
];
|
||||
try {
|
||||
const docQuery = `
|
||||
INSERT INTO documents (
|
||||
id, "urlId", title, text, "collectionId", "teamId", "parentDocumentId", "createdById",
|
||||
"lastModifiedById", template, "publishedAt", "createdAt", "updatedAt", version,
|
||||
"isWelcome", "fullWidth", "insightsEnabled"
|
||||
)
|
||||
VALUES (
|
||||
gen_random_uuid(),
|
||||
substring(replace(gen_random_uuid()::text, '-', '') from 1 for 21),
|
||||
$1, $2, $3, $4, $5, $6, $7, $8, $9, NOW(), NOW(), 1, false, false, false
|
||||
)
|
||||
RETURNING id, "urlId", title, "collectionId", "publishedAt", "createdAt"
|
||||
`;
|
||||
|
||||
const result = await pgClient.query(query, params);
|
||||
const docParams = [
|
||||
title,
|
||||
text,
|
||||
args.collection_id,
|
||||
teamId,
|
||||
args.parent_document_id || null,
|
||||
userId,
|
||||
userId,
|
||||
args.template || false,
|
||||
publishedAt
|
||||
];
|
||||
|
||||
return {
|
||||
content: [{
|
||||
type: 'text',
|
||||
text: JSON.stringify({
|
||||
success: true,
|
||||
document: result.rows[0],
|
||||
message: args.publish ? 'Documento criado e publicado' : 'Draft criado (não publicado)'
|
||||
}, null, 2)
|
||||
}]
|
||||
};
|
||||
const docResult = await pgClient.query(docQuery, docParams);
|
||||
const newDoc = docResult.rows[0];
|
||||
|
||||
// Insert initial revision (required for Outline to display the document)
|
||||
const revisionQuery = `
|
||||
INSERT INTO revisions (
|
||||
id, "documentId", "userId", title, text,
|
||||
"createdAt", "updatedAt"
|
||||
)
|
||||
VALUES (
|
||||
gen_random_uuid(), $1, $2, $3, $4, NOW(), NOW()
|
||||
)
|
||||
`;
|
||||
|
||||
await pgClient.query(revisionQuery, [
|
||||
newDoc.id,
|
||||
userId,
|
||||
title,
|
||||
text
|
||||
]);
|
||||
|
||||
await pgClient.query('COMMIT');
|
||||
|
||||
return {
|
||||
content: [{
|
||||
type: 'text',
|
||||
text: JSON.stringify({
|
||||
success: true,
|
||||
document: newDoc,
|
||||
message: args.publish ? 'Documento criado e publicado' : 'Draft criado (não publicado)'
|
||||
}, null, 2)
|
||||
}]
|
||||
};
|
||||
} catch (txError) {
|
||||
await pgClient.query('ROLLBACK');
|
||||
throw txError;
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
content: [{
|
||||
|
||||
Reference in New Issue
Block a user