Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
34 changes: 29 additions & 5 deletions src/handlers/editHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

Expand Down
43 changes: 37 additions & 6 deletions src/handlers/formHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, {
Expand All @@ -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,
Expand Down
25 changes: 19 additions & 6 deletions src/lib/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
21 changes: 21 additions & 0 deletions src/templates/emailTaken.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<div class="header">
<h1>
Adres
<a href="mailto:<?= mail ?>"><?= mail ?></a>
jest już zajęty
</h1>
</div>
<div class="content">
<h2>Czuwaj!</h2>
<p>
Dziękujemy za przesłanie wniosku o założenie konta w domenie zhr.pl. Niestety wybrany przez Ciebie adres
<strong><?= mail ?></strong> jest już zajęty przez innego użytkownika lub grupę.
</p>
<p>
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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dodaj jeszcze link do formularza

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dodano link do formularza w treści maila.

</p>
<hr />
<p>
Jeżeli uważasz, że nastąpiła pomyłka, napisz do nas w odpowiedzi na tego maila.
</p>
</div>