Reputation: 11
I'm encountering an issue with my Laravel admin panel where user reviews are duplicated with different IDs and dates. The same review content (title, body, author name) appears multiple times but with distinct review IDs and submission dates.
Review titled "Great App!" by User A appears twice with different IDs (123 and 456) and dates (June 1st and June 15th).
//Reviews fetched from the App Store using a scheduled task (up to 200 reviews/hour)
public function fetchReviews($token, $appId)
{
$curl = curl_init();
$headers = array(
"Content-Type: application/json; charset=utf-8",
"Authorization: Bearer " . $token
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.appstoreconnect.apple.com/v1/apps/$appId/customerReviews?include=response&sort=-createdDate&limit=200",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 60
));
$curlResponse = curl_exec($curl);
$result = json_decode($curlResponse);
$err = curl_error($curl);
curl_close($curl);
return $result;
}
//Store fetched reviews into the database
public function storeReviewsInDB($appId, $reviews)
{
$customerReviews = $reviews->data ?? [];
$app = DB::select('SELECT id, name FROM apps WHERE app_store_id = ?', [$appId]);
$appId = $app[0]->id;
$lastInsertedDate = DB::select(
'SELECT review_submitted_at FROM reviews WHERE app_id = ? ORDER BY review_submitted_at DESC LIMIT 1',
[$appId]
);
$lastDate = $lastInsertedDate[0]->review_submitted_at ?? null;
$insertData = [];
foreach ($customerReviews as $customerReview) {
$reviewSubmittedAt = gmdate('Y-m-d H:i:s', strtotime($customerReview->attributes->createdDate));
if (is_null($lastDate) || $reviewSubmittedAt > $lastDate) {
$responseId = $customerReview->relationships->response->data->id ?? null;
$insertData[] = [
'app_id' => $appId,
'review_id' => $customerReview->id,
'author_name' => $customerReview->attributes->reviewerNickname,
'title' => $customerReview->attributes->title,
'body' => $customerReview->attributes->body,
'rating' => $customerReview->attributes->rating,
'country' => $customerReview->attributes->territory,
'review_submitted_at' => $reviewSubmittedAt,
'response_id' => $responseId
];
}
}
if (!empty($insertData)) {
DB::table('reviews')->insert($insertData);
}
}
Has anyone faced this problem or has suggestions to resolve it? How can I ensure each review is stored only once?
Any insights would be greatly appreciated.
Upvotes: 1
Views: 54