1
0
Fork 0
mirror of synced 2024-06-10 23:04:45 +12:00

fix(api): add after pagination to logs endpoints

This commit is contained in:
Torsten Dittmann 2021-11-16 12:20:14 +01:00
parent f8731e8d65
commit 058b4dd975
3 changed files with 32 additions and 5 deletions

View file

@ -1185,13 +1185,14 @@ App::get('/v1/account/logs')
->label('sdk.response.model', Response::MODEL_LOG_LIST)
->param('limit', 25, new Range(0, 100), 'Maximum number of logs to return in response. Use this value to manage pagination. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 900000000), 'Offset value. The default value is 0. Use this param to manage pagination.', true)
->param('after', '', new UID(), 'ID of the log used as the starting point for the query, excluding the document itself. Should be used for efficient pagination when working with large sets of data.', true)
->inject('response')
->inject('user')
->inject('locale')
->inject('geodb')
->inject('dbForInternal')
->inject('usage')
->action(function ($limit, $offset, $response, $user, $locale, $geodb, $dbForInternal, $usage) {
->action(function ($limit, $offset, $after, $response, $user, $locale, $geodb, $dbForInternal, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Document $project */
/** @var Utopia\Database\Document $user */
@ -1200,6 +1201,14 @@ App::get('/v1/account/logs')
/** @var Utopia\Database\Database $dbForInternal */
/** @var Appwrite\Stats\Stats $usage */
if (!empty($after)) {
$afterLog = $dbForInternal->getDocument('audit', $after);
if ($afterLog->isEmpty()) {
throw new Exception("Log '{$after}' for the 'after' value not found.", 400);
}
}
$audit = new Audit($dbForInternal);
$logs = $audit->getLogsByUserAndEvents($user->getId(), [
@ -1218,7 +1227,7 @@ App::get('/v1/account/logs')
'teams.membership.create',
'teams.membership.update',
'teams.membership.delete',
], $limit, $offset);
], $limit, $offset, $afterLog ?? null);
$output = [];

View file

@ -460,6 +460,7 @@ App::get('/v1/database/collections/:collectionId/logs')
->param('collectionId', '', new UID(), 'Collection unique ID.')
->param('limit', 25, new Range(0, 100), 'Maximum number of logs to return in response. Use this value to manage pagination. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 900000000), 'Offset value. The default value is 0. Use this param to manage pagination.', true)
->param('after', '', new UID(), 'ID of the log used as the starting point for the query, excluding the document itself. Should be used for efficient pagination when working with large sets of data.', true)
->inject('response')
->inject('dbForInternal')
->inject('dbForExternal')
@ -479,9 +480,17 @@ App::get('/v1/database/collections/:collectionId/logs')
throw new Exception('Collection not found', 404);
}
if (!empty($after)) {
$afterLog = $dbForInternal->getDocument('audit', $after);
if ($afterLog->isEmpty()) {
throw new Exception("Log '{$after}' for the 'after' value not found.", 400);
}
}
$audit = new Audit($dbForInternal);
$logs = $audit->getLogsByResource('collection/'.$collection->getId(), $limit, $offset);
$logs = $audit->getLogsByResource('collection/'.$collection->getId(), $limit, $offset, $afterLog ?? null);
$output = [];

View file

@ -261,12 +261,13 @@ App::get('/v1/users/:userId/logs')
->param('userId', '', new UID(), 'User unique ID.')
->param('limit', 25, new Range(0, 100), 'Maximum number of logs to return in response. Use this value to manage pagination. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, new Range(0, 900000000), 'Offset value. The default value is 0. Use this param to manage pagination.', true)
->param('after', '', new UID(), 'ID of the log used as the starting point for the query, excluding the document itself. Should be used for efficient pagination when working with large sets of data.', true)
->inject('response')
->inject('dbForInternal')
->inject('locale')
->inject('geodb')
->inject('usage')
->action(function ($userId, $limit, $offset, $response, $dbForInternal, $locale, $geodb, $usage) {
->action(function ($userId, $limit, $offset, $after, $response, $dbForInternal, $locale, $geodb, $usage) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Document $project */
/** @var Utopia\Database\Database $dbForInternal */
@ -280,6 +281,14 @@ App::get('/v1/users/:userId/logs')
throw new Exception('User not found', 404);
}
if (!empty($after)) {
$afterLog = $dbForInternal->getDocument('audit', $after);
if ($afterLog->isEmpty()) {
throw new Exception("Log '{$after}' for the 'after' value not found.", 400);
}
}
$audit = new Audit($dbForInternal);
$logs = $audit->getLogsByUserAndEvents($user->getId(), [
@ -298,7 +307,7 @@ App::get('/v1/users/:userId/logs')
'teams.membership.create',
'teams.membership.update',
'teams.membership.delete',
], $limit, $offset);
], $limit, $offset, $afterLog ?? null);
$output = [];