From 2d7a1a746abbbf4ba971c08c8ea0ec2bda63a7e4 Mon Sep 17 00:00:00 2001
From: "google-labs-jules[bot]"
<161369871+google-labs-jules[bot]@users.noreply.github.com>
Date: Wed, 22 Apr 2026 08:50:20 +0000
Subject: [PATCH 1/3] feat: check if email address is already taken and handle
manual status changes
- Update `userExists` in `src/lib/user.ts` to check both users and groups in Admin Directory.
- Create `emailTaken.html` template for notifying users when their desired email is occupied.
- Update `onFormSubmit` in `src/handlers/formHandler.ts` to automatically reject requests for taken emails.
- Update `onEdit` in `src/handlers/editHandler.ts` to allow admins to trigger superior notification by manually changing status to 'Oczekiwanie na opiekuna'.
Co-authored-by: pniedzwiedzinski <19212948+pniedzwiedzinski@users.noreply.github.com>
---
src/handlers/editHandler.ts | 34 +++++++++++++++++++++++----
src/handlers/formHandler.ts | 43 ++++++++++++++++++++++++++++++-----
src/lib/user.ts | 25 +++++++++++++++-----
src/templates/emailTaken.html | 25 ++++++++++++++++++++
4 files changed, 110 insertions(+), 17 deletions(-)
create mode 100644 src/templates/emailTaken.html
diff --git a/src/handlers/editHandler.ts b/src/handlers/editHandler.ts
index 2496ede..2189eae 100644
--- a/src/handlers/editHandler.ts
+++ b/src/handlers/editHandler.ts
@@ -7,17 +7,14 @@ import {
UNIT_GROUP,
ADMIN_MAIL,
} from "../config";
+import { notifySuperior } from "./formHandler";
export function onEdit({
user,
value,
range,
}: GoogleAppsScript.Events.SheetsOnEdit) {
- if (value != "Zatwierdzono") {
- return;
- }
-
- console.info("[onEdit] Processing edit event 'Zatwierdzono'");
+ console.info(`[onEdit] Processing edit event, value: ${value}`);
const column = range.getColumn();
const row = range.getRow();
@@ -30,6 +27,33 @@ export function onEdit({
return;
}
+ if (value === "Oczekiwanie na opiekuna") {
+ const sheet = getSheet();
+ const data = getRow(sheet, row);
+ if (data.troupName) {
+ notifySuperior(
+ data.superiorEmail,
+ data.troupName,
+ "",
+ data.primaryEmail
+ );
+ } else {
+ notifySuperior(
+ data.superiorEmail,
+ data.name,
+ data.surname,
+ data.primaryEmail
+ );
+ }
+ return;
+ }
+
+ if (value != "Zatwierdzono") {
+ return;
+ }
+
+ console.info("[onEdit] Processing edit event 'Zatwierdzono'");
+
const sheet = getSheet();
const userToCreate = getRow(sheet, row);
diff --git a/src/handlers/formHandler.ts b/src/handlers/formHandler.ts
index 0384bef..de21ea3 100644
--- a/src/handlers/formHandler.ts
+++ b/src/handlers/formHandler.ts
@@ -2,6 +2,7 @@ import { getSheet, getRow, updateRow } from "../lib/sheet";
import { proposeEmail } from "../lib/utils";
import { sendEmail, renderTemplate } from "../lib/utils";
import { PROXY_URL } from "../config";
+import { userExists } from "../lib/user";
/**
* Handles the event
@@ -10,14 +11,44 @@ export function onFormSubmit(e: GoogleAppsScript.Events.SheetsOnFormSubmit) {
console.info("[onFormSubmit] Started handling form submission");
const row = e.range.getRow();
const sheet = getSheet();
- let { name, surname, troupName, superiorEmail, primaryEmail } = getRow(
- sheet,
- row
- );
+ let {
+ name,
+ surname,
+ troupName,
+ superiorEmail,
+ primaryEmail,
+ recoveryEmail,
+ } = getRow(sheet, row);
+
+ if (!troupName) {
+ primaryEmail = `${proposeEmail(name, surname)}@zhr.pl`;
+ }
+
+ if (userExists(primaryEmail)) {
+ console.info(
+ `[onFormSubmit] Email ${primaryEmail} is already taken. Rejecting request.`
+ );
+ const subject = `Wybrany adres ${primaryEmail} jest już zajęty`;
+ const htmlBody = renderTemplate(
+ "emailTaken",
+ { mail: primaryEmail },
+ subject
+ ).getContent();
+ sendEmail(recoveryEmail, subject, "", {
+ htmlBody,
+ });
+
+ updateRow(sheet, row, {
+ status: "Odmówiono",
+ primaryEmail,
+ isUnit: !!troupName,
+ });
+ return;
+ }
+
if (troupName) {
notifySuperior(superiorEmail, troupName, "", primaryEmail);
} else {
- primaryEmail = `${proposeEmail(name, surname)}@zhr.pl`;
notifySuperior(superiorEmail, name, surname, primaryEmail);
}
updateRow(sheet, row, {
@@ -31,7 +62,7 @@ export function onFormSubmit(e: GoogleAppsScript.Events.SheetsOnFormSubmit) {
/**
* Send email with superior confirmation link
*/
-function notifySuperior(
+export function notifySuperior(
superiorEmail: string,
name: string,
surname: string,
diff --git a/src/lib/user.ts b/src/lib/user.ts
index 77068c2..019ed8d 100644
--- a/src/lib/user.ts
+++ b/src/lib/user.ts
@@ -74,17 +74,30 @@ export function getGoogleUserSafe(
}
}
-function userExists(mail: string) {
+export function userExists(mail: string) {
+ if (!AdminDirectory) {
+ throw new Error("AdminDirectory is undefined");
+ }
+
try {
- if (AdminDirectory && AdminDirectory.Users) {
+ if (AdminDirectory.Users) {
AdminDirectory.Users.get(mail);
- } else {
- throw new Error("AdminDirectory.Users is undefined");
+ return true;
+ }
+ } catch (err) {
+ // User not found, continue to check groups
+ }
+
+ try {
+ if (AdminDirectory.Groups) {
+ AdminDirectory.Groups.get(mail);
+ return true;
}
} catch (err) {
- return false;
+ // Group not found
}
- return true;
+
+ return false;
}
export function createUser(
diff --git a/src/templates/emailTaken.html b/src/templates/emailTaken.html
new file mode 100644
index 0000000..72c52d0
--- /dev/null
+++ b/src/templates/emailTaken.html
@@ -0,0 +1,25 @@
+
+
+
Czuwaj!
+
+ Dziękujemy za przesłanie wniosku o założenie konta w domenie zhr.pl. Niestety wybrany przez Ciebie adres
+ = mail ?> jest już zajęty przez innego użytkownika lub grupę.
+
+
+ Twój wniosek został automatycznie odrzucony. Prosimy o kontakt z administratorem w celu ustalenia unikalnego adresu e-mail.
+ Możesz odpowiedzieć bezpośrednio na tę wiadomość.
+
+
+ Po ustaleniu nowego adresu, administrator wprowadzi zmiany i prześle wniosek do ponownej weryfikacji.
+
+
+
+ Jeżeli uważasz, że nastąpiła pomyłka, napisz do nas w odpowiedzi na tego maila.
+
+
From 5d2ab4fbe4e508acb7b547f989d266d55955f58d Mon Sep 17 00:00:00 2001
From: "google-labs-jules[bot]"
<161369871+google-labs-jules[bot]@users.noreply.github.com>
Date: Wed, 22 Apr 2026 12:16:09 +0000
Subject: [PATCH 2/3] refactor: update emailTaken template based on PR feedback
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- Change header to 'Adres (adres) jest już zajęty'
- Update rejection message to mention form resubmission
- Remove redundant paragraph about admin edits
Co-authored-by: pniedzwiedzinski <19212948+pniedzwiedzinski@users.noreply.github.com>
---
src/templates/emailTaken.html | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/src/templates/emailTaken.html b/src/templates/emailTaken.html
index 72c52d0..6f6ce54 100644
--- a/src/templates/emailTaken.html
+++ b/src/templates/emailTaken.html
@@ -1,6 +1,6 @@