-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add private route for restore workbook #31
Merged
Sergey-weber
merged 13 commits into
main
from
CHARTS-8714-add-private-route-for-restore-workbook
Dec 7, 2023
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2b20693
add new route, controller for restore workbook
Sergey-weber a47bbd3
Added where DeletedAt depends on workbook deletedAt
Sergey-weber ba4335e
Add int tests
Sergey-weber e33fbaa
Add praparing for test
Sergey-weber 819ae97
Fix test
Sergey-weber f64bf0c
Fix test
Sergey-weber fff6941
Add deleting workbook test
Sergey-weber 22f50db
Fix test deleting
Sergey-weber 76aab08
Add test for getting entries workbook after restore
Sergey-weber 79bc8e2
Merge branch 'main' into CHARTS-8714-add-private-route-for-restore-wo…
Sergey-weber a24a2cf
Add new error - WORKBOOK_IS_ALREADY_RESTORED
Sergey-weber 5d3a0ba
Fix after review
Sergey-weber 806b6f6
Merge branch 'main' into CHARTS-8714-add-private-route-for-restore-wo…
Sergey-weber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
7 changes: 7 additions & 0 deletions
7
src/services/new/workbook/formatters/format-restore-workbook.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,7 @@ | ||
import {WorkbookModel} from '../../../../db/models/new/workbook'; | ||
|
||
export const formatRestoreWorkbook = (workbookModel: WorkbookModel) => { | ||
return { | ||
workbookId: workbookModel.workbookId, | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import {transaction, raw} from 'objection'; | ||
import {AppError} from '@gravity-ui/nodekit'; | ||
import {ServiceArgs} from '../types'; | ||
import {getReplica, getPrimary} from '../utils'; | ||
import {makeSchemaValidator} from '../../../components/validation-schema-compiler'; | ||
import {WorkbookModel, WorkbookModelColumn} from '../../../db/models/new/workbook'; | ||
import Utils, {logInfo} from '../../../utils'; | ||
import {Entry, EntryColumn} from '../../../db/models/new/entry'; | ||
|
||
import {US_ERRORS, CURRENT_TIMESTAMP, DEFAULT_QUERY_TIMEOUT, TRASH_FOLDER} from '../../../const'; | ||
|
||
const validateArgs = makeSchemaValidator({ | ||
type: 'object', | ||
required: ['workbookId'], | ||
properties: { | ||
workbookId: { | ||
type: 'string', | ||
}, | ||
}, | ||
}); | ||
|
||
export interface RestoreWorkbookArgs { | ||
workbookId: string; | ||
} | ||
|
||
export const restoreWorkbook = async ( | ||
{ctx, trx, skipValidation = false}: ServiceArgs, | ||
args: RestoreWorkbookArgs, | ||
) => { | ||
const {workbookId} = args; | ||
|
||
logInfo(ctx, 'RESTORE_WORKBOOK_START', { | ||
workbookId: Utils.encodeId(workbookId), | ||
}); | ||
|
||
if (!skipValidation) { | ||
validateArgs(args); | ||
} | ||
|
||
const {tenantId} = ctx.get('info'); | ||
|
||
const targetTrx = getReplica(trx); | ||
|
||
const model: Optional<WorkbookModel> = await WorkbookModel.query(targetTrx) | ||
.select() | ||
.skipUndefined() | ||
.where({ | ||
[WorkbookModelColumn.TenantId]: tenantId, | ||
[WorkbookModelColumn.WorkbookId]: workbookId, | ||
}) | ||
.andWhereNot(WorkbookModelColumn.DeletedAt, null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's check this after the query, like: if (!model) {
throw new AppError(US_ERRORS.WORKBOOK_NOT_EXISTS, {
code: US_ERRORS.WORKBOOK_NOT_EXISTS,
});
}
if (model.deletedAt !== null) {
throw new AppError(US_ERRORS.WORKBOOK_IS_ALREADY_RESTORED, { // new error with 400 status
code: US_ERRORS.WORKBOOK_IS_ALREADY_RESTORED,
});
} |
||
.first() | ||
.timeout(WorkbookModel.DEFAULT_QUERY_TIMEOUT); | ||
|
||
if (!model) { | ||
throw new AppError(US_ERRORS.WORKBOOK_NOT_EXISTS, { | ||
code: US_ERRORS.WORKBOOK_NOT_EXISTS, | ||
}); | ||
} | ||
|
||
const entries = await Entry.query(targetTrx) | ||
.select() | ||
.skipUndefined() | ||
.where({ | ||
[EntryColumn.WorkbookId]: workbookId, | ||
[EntryColumn.TenantId]: tenantId, | ||
[EntryColumn.IsDeleted]: true, | ||
}) | ||
.timeout(Entry.DEFAULT_QUERY_TIMEOUT); | ||
|
||
const primaryTrx = getPrimary(trx); | ||
|
||
const result = await transaction(primaryTrx, async (transactionTrx) => { | ||
const restoredWorkbook = await WorkbookModel.query(transactionTrx) | ||
.skipUndefined() | ||
.patch({ | ||
[WorkbookModelColumn.DeletedBy]: null, | ||
[WorkbookModelColumn.DeletedAt]: null, | ||
[WorkbookModelColumn.UpdatedAt]: raw(CURRENT_TIMESTAMP), | ||
}) | ||
.where({ | ||
[WorkbookModelColumn.WorkbookId]: model.workbookId, | ||
[WorkbookModelColumn.TenantId]: tenantId, | ||
}) | ||
.returning('*') | ||
.first() | ||
.timeout(WorkbookModel.DEFAULT_QUERY_TIMEOUT); | ||
|
||
await Promise.all( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can restore all the entries by one patch query: UPDATE entries SET
key = regexp_replace(key, '__trash/', ''),
display_key = regexp_replace(display_key, '__trash/', ''),
inner_meta = inner_meta - 'oldKey' - 'oldDisplayKey',
is_deleted = FALSE,
deleted_at = NULL,
updated_at = NOW()
WHERE
tenant_id = '{TENANT_ID}' AND workbook_id = {WORKBOOK_ID} AND deleted_at > '{WORKBOOK_DELETED_AT}'; And getting all the entries above will be unnecessary. |
||
entries.map(async (entry) => { | ||
const {entryId, displayKey, key} = entry; | ||
|
||
const newInnerMeta = { | ||
...entry.innerMeta, | ||
oldKey: key as string, | ||
oldDisplayKey: displayKey as string, | ||
}; | ||
|
||
return await Entry.query(transactionTrx) | ||
.patch({ | ||
key: key?.replace(TRASH_FOLDER + '/', ''), | ||
displayKey: displayKey?.replace(TRASH_FOLDER + '/', ''), | ||
innerMeta: newInnerMeta, | ||
isDeleted: false, | ||
deletedAt: null, | ||
updatedAt: raw(CURRENT_TIMESTAMP), | ||
}) | ||
.where({ | ||
entryId, | ||
}) | ||
.andWhere(EntryColumn.DeletedAt, '>=', model.deletedAt) | ||
.timeout(DEFAULT_QUERY_TIMEOUT); | ||
}), | ||
); | ||
|
||
return restoredWorkbook; | ||
}); | ||
|
||
if (!result) { | ||
throw new AppError(US_ERRORS.WORKBOOK_NOT_EXISTS, { | ||
code: US_ERRORS.WORKBOOK_NOT_EXISTS, | ||
}); | ||
} | ||
|
||
ctx.log('RESTORE_WORKBOOK_FINISH', { | ||
workbookId: Utils.encodeId(workbookId), | ||
}); | ||
|
||
return result; | ||
}; |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it needed here and below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnessesary, fixed it, was error when don't have tenantId