Skip to content
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

Prevent creation of duplicate export requests #1150

Merged
merged 1 commit into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions classes/DataWarehouse/Export/QueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,44 @@ public function createRequestRecord(
$endDate,
$format
) {
$sql = "INSERT INTO batch_export_requests
(requested_datetime, user_id, realm, start_date, end_date, export_file_format)
VALUES
(NOW(), :user_id, :realm, :start_date, :end_date, :export_file_format)";
try {
$this->dbh->beginTransaction();

$params = array(
'user_id' => $userId,
'realm' => $realm,
'start_date' => $startDate,
'end_date' => $endDate,
'export_file_format' => $format
);
// Check for duplicate submitted or available requests for the user.
$duplicates = array_filter(
$this->listUserRequestsByState($userId),
function ($request) use ($realm, $startDate, $endDate, $format) {
return ($request['state'] == 'Submitted' || $request['state'] == 'Available')
&& $realm == $request['realm']
&& $startDate == $request['start_date']
&& $endDate == $request['end_date']
&& $format == $request['export_file_format'];
}
);
if (count($duplicates) > 0) {
throw new Exception('Cannot create duplicate request');
}

$sql = "INSERT INTO batch_export_requests
(requested_datetime, user_id, realm, start_date, end_date, export_file_format)
VALUES
(NOW(), :user_id, :realm, :start_date, :end_date, :export_file_format)";

return $this->dbh->insert($sql, $params);
$params = array(
'user_id' => $userId,
'realm' => $realm,
'start_date' => $startDate,
'end_date' => $endDate,
'export_file_format' => $format
);

$id = $this->dbh->insert($sql, $params);
$this->dbh->commit();
return $id;
} catch (Exception $e) {
$this->dbh->rollBack();
throw $e;
}
}

/**
Expand Down
18 changes: 11 additions & 7 deletions classes/Rest/Controllers/WarehouseExportControllerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,17 @@ function ($realm) {
throw new BadRequestHttpException('format must be CSV or JSON');
}

$id = $this->queryHandler->createRequestRecord(
$user->getUserId(),
$realm,
$startDate->format('Y-m-d'),
$endDate->format('Y-m-d'),
$format
);
try {
$id = $this->queryHandler->createRequestRecord(
$user->getUserId(),
$realm,
$startDate->format('Y-m-d'),
$endDate->format('Y-m-d'),
$format
);
} catch (Exception $e) {
throw new BadRequestHttpException('Failed to create export request: ' . $e->getMessage());
}

return $app->json([
'success' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
200,
"POST-request"
],
"Normal user (duplicate)": [
"usr",
{
"realm": "jobs",
"start_date": "2017-02-01",
"end_date": "2017-02-28",
"format": "JSON"
},
400,
"error"
],
"PI": [
"pi",
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"usr",
{
"realm": "jobs",
"start_date": "2018-01-01",
"end_date": "2018-12-31",
"start_date": "2018-02-01",
"end_date": "2018-02-28",
"format": "CSV"
},
200,
Expand All @@ -14,8 +14,8 @@
"pi",
{
"realm": "jobs",
"start_date": "2018-01-01",
"end_date": "2018-12-31",
"start_date": "2018-02-01",
"end_date": "2018-02-28",
"format": "CSV"
},
200,
Expand All @@ -25,8 +25,8 @@
"cd",
{
"realm": "jobs",
"start_date": "2018-01-01",
"end_date": "2018-12-31",
"start_date": "2018-02-01",
"end_date": "2018-02-28",
"format": "CSV"
},
200,
Expand Down