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..a1fd207 100644 --- a/src/handlers/formHandler.ts +++ b/src/handlers/formHandler.ts @@ -1,7 +1,8 @@ import { getSheet, getRow, updateRow } from "../lib/sheet"; import { proposeEmail } from "../lib/utils"; import { sendEmail, renderTemplate } from "../lib/utils"; -import { PROXY_URL } from "../config"; +import { PROXY_URL, SURVEY_LINK } 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, surveyLink: SURVEY_LINK }, + 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..e271f0d --- /dev/null +++ b/src/templates/emailTaken.html @@ -0,0 +1,22 @@ +
+

+ Adres + + jest już zajęty +

+
+
+

Czuwaj!

+

+ Dziękujemy za przesłanie wniosku o założenie konta w domenie zhr.pl. Niestety wybrany przez Ciebie adres + jest już zajęty przez innego użytkownika lub grupę. +

+

+ Twój wniosek został automatycznie odrzucony. Możesz uzupełnić formularz jeszcze raz lub skontaktować się z administratorem z prośbą o nadanie unikalnego adresu e-mail. +

+ Założenie skrzynki mailowej @zhr +
+

+ Jeżeli uważasz, że nastąpiła pomyłka, napisz do nas w odpowiedzi na tego maila. +

+