-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
180 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use server' | ||
|
||
import { CommitsRepository } from '@latitude-data/core/repositories' | ||
import { z } from 'zod' | ||
|
||
import { withProject } from '../procedures' | ||
import { renameDocumentPaths } from '@latitude-data/core/services/documents/renameDocumentPaths' | ||
|
||
export const renameDocumentPathsAction = withProject | ||
.createServerAction() | ||
.input( | ||
z.object({ | ||
commitUuid: z.string(), | ||
oldPath: z.string(), | ||
newPath: z.string(), | ||
}), | ||
{ type: 'json' }, | ||
) | ||
.handler(async ({ input, ctx }) => { | ||
const commitsScope = new CommitsRepository(ctx.project.workspaceId) | ||
const commit = await commitsScope | ||
.getCommitByUuid({ uuid: input.commitUuid, projectId: ctx.project.id }) | ||
.then((r) => r.unwrap()) | ||
|
||
const result = await renameDocumentPaths({ | ||
commit, | ||
oldPath: input.oldPath, | ||
newPath: input.newPath, | ||
}) | ||
|
||
return result.unwrap() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/core/src/services/documents/renameDocumentPaths.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { Commit, DocumentVersion } from '../../browser' | ||
import { database } from '../../client' | ||
import { findWorkspaceFromCommit } from '../../data-access' | ||
import { Result, Transaction, TypedResult } from '../../lib' | ||
import { BadRequestError } from '../../lib/errors' | ||
import { DocumentVersionsRepository } from '../../repositories' | ||
import { updateDocument } from './update' | ||
|
||
export async function renameDocumentPaths( | ||
{ | ||
commit, | ||
oldPath, | ||
newPath, | ||
}: { | ||
commit: Commit | ||
oldPath: string | ||
newPath: string | ||
}, | ||
db = database, | ||
): Promise<TypedResult<DocumentVersion[], Error>> { | ||
return await Transaction.call(async (tx) => { | ||
if (commit.mergedAt !== null) { | ||
return Result.error(new BadRequestError('Cannot modify a merged commit')) | ||
} | ||
|
||
const workspace = await findWorkspaceFromCommit(commit, tx) | ||
const docsScope = new DocumentVersionsRepository(workspace!.id, tx) | ||
|
||
const currentDocs = await docsScope | ||
.getDocumentsAtCommit(commit) | ||
.then((r) => r.unwrap()) | ||
|
||
const docsToUpdate = currentDocs.filter((d) => | ||
oldPath.endsWith('/') ? d.path.startsWith(oldPath) : d.path === oldPath, | ||
) | ||
|
||
const updatedDocs = await Promise.all( | ||
docsToUpdate.map(async (document) => { | ||
const updatedPath = document.path.replace(oldPath, newPath) | ||
const updatedDoc = await updateDocument({ | ||
commit, | ||
document, | ||
path: updatedPath, | ||
}) | ||
return updatedDoc.unwrap() | ||
}), | ||
) | ||
|
||
return Result.ok(updatedDocs) | ||
}, db) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.