public inbox for [email protected]
 help / color / mirror / Atom feed
From: Ammar Faizi <[email protected]>
To: Gilang Fachrezy <[email protected]>
Cc: Ammar Faizi <[email protected]>,
	Taufiq Pohan <[email protected]>,
	Aldy Prastyo <[email protected]>,
	Muhammad Fitrah Pandjalu <[email protected]>,
	Nauvalsa Yanandana <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>,
	VNLX Kernel Department <[email protected]>
Subject: [PATCH v1 13/13] index: Make sure there is no duplicate submission in the same day
Date: Mon, 28 Nov 2022 03:32:16 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

It's possible that a user resubmit the same data in the same day.
Avoid it!

Co-authored-by: Muhammad Fitrah Pandjalu <[email protected]>
Signed-off-by: Muhammad Fitrah Pandjalu <[email protected]>
Co-authored-by: Taufiq Pohan <[email protected]>
Signed-off-by: Taufiq Pohan <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 public/api.php   | 21 ++++++++++++++++++++-
 public/index.php |  2 +-
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/public/api.php b/public/api.php
index 8305552..7250075 100644
--- a/public/api.php
+++ b/public/api.php
@@ -2,7 +2,7 @@
 
 require __DIR__."/../helpers.php";
 
-date_default_timezone_set("UTC");
+date_default_timezone_set("Asia/Jakarta");
 
 function err_msg(int $code, string $msg): array
 {
@@ -25,6 +25,21 @@ const SOCIAL_MEDIA = [
 	"github_username",
 ];
 
+function has_email_been_saved(PDO $pdo, string $email): bool
+{
+	$st = $pdo->prepare(<<<SQL
+		SELECT EXISTS (
+			SELECT * FROM attendances WHERE
+			email = ?
+			AND DAY(created_at) = ?
+			AND MONTH(created_at) = ?
+			AND YEAR(created_at) = ?
+		);
+	SQL);
+	$st->execute([$email, (int)date("d"), (int)date("m"), date("Y")]);
+	return (bool)(int)$st->fetch(PDO::FETCH_NUM)[0];
+}
+
 function submit_attendance(): array
 {
 	if ($_SERVER["REQUEST_METHOD"] !== "POST")
@@ -62,6 +77,10 @@ function submit_attendance(): array
 
 	try {
 		$pdo = pdo();
+
+		if (has_email_been_saved($pdo, $j["email"]))
+			return [400, err_msg(400, "Your data has already been recorded, please don't submit a duplicate submission!")];
+
 		$st = $pdo->prepare(<<<SQL
 			INSERT INTO `attendances`
 			(
diff --git a/public/index.php b/public/index.php
index e07f750..b297f56 100644
--- a/public/index.php
+++ b/public/index.php
@@ -160,7 +160,7 @@
 					let j = res.responseJSON;
 
 					if ("error" in j)
-						form_err(j.error, "Server Error");
+						form_err(j.error, "Bad Request");
 					else
 						form_err("Unknown error!", "Server Error");
 				}
-- 
Ammar Faizi


      parent reply	other threads:[~2022-11-27 20:33 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-27 20:32 [PATCH v1 00/13] App for member attendance record at Senayan Park, Jakarta Ammar Faizi
2022-11-27 20:32 ` [PATCH v1 01/13] Initial index page Ammar Faizi
2022-11-27 20:32 ` [PATCH v1 02/13] Add regions.json Ammar Faizi
2022-11-27 20:32 ` [PATCH v1 03/13] index: Integrate city data with the form Ammar Faizi
2022-11-27 20:32 ` [PATCH v1 04/13] index: Add social media accounts input Ammar Faizi
2022-11-27 20:32 ` [PATCH v1 05/13] index: Add a red star to the required fields Ammar Faizi
2022-11-27 20:32 ` [PATCH v1 06/13] index: city: Add "select the city" option on blank form Ammar Faizi
2022-11-27 20:32 ` [PATCH v1 07/13] Export the DDL Ammar Faizi
2022-11-27 20:32 ` [PATCH v1 08/13] Initial work on the database integration Ammar Faizi
2022-11-27 20:32 ` [PATCH v1 09/13] Create initial API integration Ammar Faizi
2022-11-27 20:32 ` [PATCH v1 10/13] Add input form validation Ammar Faizi
2022-11-27 20:32 ` [PATCH v1 11/13] assets: Add sweetalert library Ammar Faizi
2022-11-27 20:32 ` [PATCH v1 12/13] index: Integrate the sweetalert library with the form Ammar Faizi
2022-11-27 20:32 ` Ammar Faizi [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox