feat: contacts as pills on a white card; remove several at once
- Contacts list sits in the same white card as the Shifts panels, and each contact is a light-blue pill like a shift row. Stacks cleanly on phones. - Each contact has a checkbox, with Select all and Remove selected. The confirm dialog says how many contacts will be removed. - Select all only ticks contacts currently shown, never rows still behind "See more"; revealing more rows leaves Select all partial, not extended. This applies to every picker but changes nothing where no rows are hidden. - POST /admin/contacts/remove: session, origin and CSRF checked; one transaction; if any contact is missing, none are removed; one audit row. Signups are never touched and no email is sent. - tests: 5 new contact tests.
This commit is contained in:
1 parent
bf3724d531
commit
b142dd9748
7 files changed
+324
-18
No files matched your search
+96
-9
@@ -3367,7 +3367,8 @@ button.btn-quiet::before {
|
|||||||
gap: 1rem;
|
gap: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.coordinator-board .desk-panel {
|
.coordinator-board .desk-panel,
|
||||||
|
.contacts-board {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: 0.8rem;
|
gap: 0.8rem;
|
||||||
@@ -3380,7 +3381,8 @@ button.btn-quiet::before {
|
|||||||
box-shadow: 0 8px 24px rgb(34 55 70 / 0.05);
|
box-shadow: 0 8px 24px rgb(34 55 70 / 0.05);
|
||||||
}
|
}
|
||||||
|
|
||||||
.coordinator-board .desk-panel h2 {
|
.coordinator-board .desk-panel h2,
|
||||||
|
.contacts-board h2 {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
font-size: 1.35rem;
|
font-size: 1.35rem;
|
||||||
font-weight: 650;
|
font-weight: 650;
|
||||||
@@ -3688,7 +3690,8 @@ button.btn-quiet::before {
|
|||||||
stroke-linejoin: round;
|
stroke-linejoin: round;
|
||||||
}
|
}
|
||||||
|
|
||||||
.coordinator-board .desk-panel .empty {
|
.coordinator-board .desk-panel .empty,
|
||||||
|
.contacts-board .empty {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0.85rem 1rem;
|
padding: 0.85rem 1rem;
|
||||||
background: #f3f7fb;
|
background: #f3f7fb;
|
||||||
@@ -3815,19 +3818,29 @@ button.btn-quiet::before {
|
|||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Contact rows: light-blue pills on the white panel, like the shift rows. */
|
||||||
.contact-list {
|
.contact-list {
|
||||||
list-style: none;
|
list-style: none;
|
||||||
margin: 1rem 0 0;
|
display: grid;
|
||||||
|
gap: 0.55rem;
|
||||||
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.contact-row {
|
.contact-row {
|
||||||
display: grid;
|
display: flex;
|
||||||
grid-template-columns: minmax(10rem, 1.2fr) minmax(12rem, 1.6fr) minmax(7rem, 0.8fr) auto;
|
flex-wrap: wrap;
|
||||||
|
align-items: center;
|
||||||
gap: 0.35rem 1rem;
|
gap: 0.35rem 1rem;
|
||||||
align-items: baseline;
|
min-height: 4.15rem;
|
||||||
padding: 0.75rem 0;
|
padding: 0.7rem 0.95rem;
|
||||||
border-top: 1px solid var(--hope-border-2);
|
background: #f3f7fb;
|
||||||
|
border-radius: 0.95rem;
|
||||||
|
color: var(--hope-ink);
|
||||||
|
}
|
||||||
|
|
||||||
|
.contact-row:hover {
|
||||||
|
background: #e8f1f7;
|
||||||
}
|
}
|
||||||
|
|
||||||
.contact-row p {
|
.contact-row p {
|
||||||
@@ -3839,6 +3852,80 @@ button.btn-quiet::before {
|
|||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.contact-row .pick {
|
||||||
|
flex: none;
|
||||||
|
justify-content: center;
|
||||||
|
min-height: 2.4rem;
|
||||||
|
padding: 0 0.3rem;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.contact-row .pick input {
|
||||||
|
width: 1.25rem;
|
||||||
|
height: 1.25rem;
|
||||||
|
margin: 0;
|
||||||
|
accent-color: var(--hope-ink);
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.contact-name {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
flex: 1 1 10rem;
|
||||||
|
min-width: 8rem;
|
||||||
|
margin: 0;
|
||||||
|
font-size: 0.98rem;
|
||||||
|
line-height: 1.3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.contact-name a {
|
||||||
|
color: inherit;
|
||||||
|
font-weight: 650;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.contact-name a:hover,
|
||||||
|
.contact-name a:active {
|
||||||
|
text-decoration: underline;
|
||||||
|
text-underline-offset: 0.12em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.contact-email {
|
||||||
|
flex: 1 1 13rem;
|
||||||
|
min-width: 0;
|
||||||
|
margin: 0;
|
||||||
|
color: #7d8b99;
|
||||||
|
font-size: var(--hope-text-small);
|
||||||
|
line-height: 1.3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.contact-phone {
|
||||||
|
flex: 0 1 auto;
|
||||||
|
margin: 0;
|
||||||
|
color: #7d8b99;
|
||||||
|
font-size: var(--hope-text-small);
|
||||||
|
line-height: 1.3;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.contact-remove {
|
||||||
|
flex: none;
|
||||||
|
margin: 0 0 0 auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 42em) {
|
||||||
|
.contact-name {
|
||||||
|
flex: 1 1 auto;
|
||||||
|
min-width: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.contact-email,
|
||||||
|
.contact-phone {
|
||||||
|
flex: 1 1 100%;
|
||||||
|
white-space: normal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.contact-actions {
|
.contact-actions {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: repeat(3, max-content) minmax(0, 1fr);
|
grid-template-columns: repeat(3, max-content) minmax(0, 1fr);
|
||||||
|
|||||||
@@ -15,12 +15,23 @@
|
|||||||
return link.getAttribute(name) || '';
|
return link.getAttribute(name) || '';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// A box inside a [hidden] subtree is not shown to the user (rows still behind "See more"), so it never counts.
|
||||||
|
var visibleBoxes = function (selector) {
|
||||||
|
var all = document.querySelectorAll(selector);
|
||||||
|
var shown = [];
|
||||||
|
for (var i = 0; i < all.length; i += 1) {
|
||||||
|
if (all[i].closest('[hidden]')) continue;
|
||||||
|
shown.push(all[i]);
|
||||||
|
}
|
||||||
|
return shown;
|
||||||
|
};
|
||||||
|
|
||||||
var chosenFor = function (name) {
|
var chosenFor = function (name) {
|
||||||
return document.querySelectorAll('input[data-pick="' + name + '"]:checked');
|
return visibleBoxes('input[data-pick="' + name + '"]:checked');
|
||||||
};
|
};
|
||||||
|
|
||||||
var boxesFor = function (name) {
|
var boxesFor = function (name) {
|
||||||
return document.querySelectorAll('input[data-pick="' + name + '"]');
|
return visibleBoxes('input[data-pick="' + name + '"]');
|
||||||
};
|
};
|
||||||
|
|
||||||
var fill = function (link) {
|
var fill = function (link) {
|
||||||
@@ -88,7 +99,12 @@
|
|||||||
if (blocked > 0 && kept.length > 0) {
|
if (blocked > 0 && kept.length > 0) {
|
||||||
detail.textContent = kept.length + ' of ' + chosen.length + ' can be ' + verb;
|
detail.textContent = kept.length + ' of ' + chosen.length + ' can be ' + verb;
|
||||||
} else {
|
} else {
|
||||||
detail.textContent = chosen.length === 1 ? '1 selected' : chosen.length + ' selected';
|
var unit = text(link, 'data-confirm-unit');
|
||||||
|
if (unit) {
|
||||||
|
detail.textContent = chosen.length === 1 ? '1 ' + unit + ' will be removed.' : chosen.length + ' ' + unit + 's will be removed.';
|
||||||
|
} else {
|
||||||
|
detail.textContent = chosen.length === 1 ? '1 selected' : chosen.length + ' selected';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
detail.hidden = false;
|
detail.hidden = false;
|
||||||
if (weekly && weeklyNote) notes.push(weeklyNote);
|
if (weekly && weeklyNote) notes.push(weeklyNote);
|
||||||
@@ -109,7 +125,8 @@
|
|||||||
var syncPicks = function () {
|
var syncPicks = function () {
|
||||||
document.querySelectorAll('[data-confirm-pick]').forEach(function (control) {
|
document.querySelectorAll('[data-confirm-pick]').forEach(function (control) {
|
||||||
var name = control.getAttribute('data-confirm-pick');
|
var name = control.getAttribute('data-confirm-pick');
|
||||||
control.hidden = !name || chosenFor(name).length === 0;
|
var hide = !name || chosenFor(name).length === 0;
|
||||||
|
if (control.hidden !== hide) control.hidden = hide;
|
||||||
});
|
});
|
||||||
document.querySelectorAll('[data-pick-all]').forEach(function (all) {
|
document.querySelectorAll('[data-pick-all]').forEach(function (all) {
|
||||||
var name = all.getAttribute('data-pick-all');
|
var name = all.getAttribute('data-pick-all');
|
||||||
@@ -163,5 +180,10 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
syncPicks();
|
syncPicks();
|
||||||
|
// Re-check the pick state when rows appear or disappear, e.g. "See more" revealing contacts.
|
||||||
|
// The guarded writes in syncPicks keep this from looping.
|
||||||
|
if (typeof MutationObserver === 'function') {
|
||||||
|
new MutationObserver(syncPicks).observe(document.body, { attributes: true, attributeFilter: ['hidden'], subtree: true });
|
||||||
|
}
|
||||||
if (dialog.open) show();
|
if (dialog.open) show();
|
||||||
})();
|
})();
|
||||||
@@ -91,6 +91,7 @@ import {
|
|||||||
loadContacts,
|
loadContacts,
|
||||||
parseContactSkips,
|
parseContactSkips,
|
||||||
removeContact,
|
removeContact,
|
||||||
|
removeContacts,
|
||||||
saveManualContact,
|
saveManualContact,
|
||||||
contactSignupShifts,
|
contactSignupShifts,
|
||||||
walkInFromContact,
|
walkInFromContact,
|
||||||
@@ -154,6 +155,7 @@ export function registerAdmin(app: Hono, services: AdminServices): void {
|
|||||||
app.post(appPath(config, '/admin/contacts/new'), (c) => submitNewContact(c, services));
|
app.post(appPath(config, '/admin/contacts/new'), (c) => submitNewContact(c, services));
|
||||||
app.post(appPath(config, '/admin/contacts/import'), (c) => submitImportContacts(c, services));
|
app.post(appPath(config, '/admin/contacts/import'), (c) => submitImportContacts(c, services));
|
||||||
app.post(appPath(config, '/admin/contacts/export'), (c) => submitExportContacts(c, services));
|
app.post(appPath(config, '/admin/contacts/export'), (c) => submitExportContacts(c, services));
|
||||||
|
app.post(appPath(config, '/admin/contacts/remove'), (c) => submitRemoveContacts(c, services));
|
||||||
app.get(appPath(config, '/admin/contacts/:id/remove'), (c) => showRemoveContact(c, services));
|
app.get(appPath(config, '/admin/contacts/:id/remove'), (c) => showRemoveContact(c, services));
|
||||||
app.post(appPath(config, '/admin/contacts/:id/remove'), (c) => submitRemoveContact(c, services));
|
app.post(appPath(config, '/admin/contacts/:id/remove'), (c) => submitRemoveContact(c, services));
|
||||||
app.post(appPath(config, '/admin/contacts/:id/add'), (c) => submitAddContactToShift(c, services));
|
app.post(appPath(config, '/admin/contacts/:id/add'), (c) => submitAddContactToShift(c, services));
|
||||||
@@ -364,6 +366,7 @@ function showContacts(
|
|||||||
services: AdminServices,
|
services: AdminServices,
|
||||||
options?: {
|
options?: {
|
||||||
error?: string;
|
error?: string;
|
||||||
|
removeError?: string;
|
||||||
status?: 200 | 400 | 403;
|
status?: 200 | 400 | 403;
|
||||||
confirm?: ConfirmPrompt;
|
confirm?: ConfirmPrompt;
|
||||||
manual?: { values: ContactManualValues; errors: ContactManualErrors };
|
manual?: { values: ContactManualValues; errors: ContactManualErrors };
|
||||||
@@ -384,6 +387,8 @@ function showContacts(
|
|||||||
query={loaded.query}
|
query={loaded.query}
|
||||||
limit={contactListLimit(c.req.query('shown'))}
|
limit={contactListLimit(c.req.query('shown'))}
|
||||||
notice={options?.error ? undefined : knownNotice(c.req.query('notice'))}
|
notice={options?.error ? undefined : knownNotice(c.req.query('notice'))}
|
||||||
|
noticeText={contactsRemovedNotice(c.req.query('removed'))}
|
||||||
|
removeError={options?.removeError}
|
||||||
summary={options?.error ? undefined : contactSummary(c)}
|
summary={options?.error ? undefined : contactSummary(c)}
|
||||||
importError={options?.error}
|
importError={options?.error}
|
||||||
manual={options?.manual}
|
manual={options?.manual}
|
||||||
@@ -600,6 +605,20 @@ async function submitRemoveContact(c: Context, services: AdminServices): Promise
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function submitRemoveContacts(c: Context, services: AdminServices): Promise<Response> {
|
||||||
|
const session = await postedSession(c, services);
|
||||||
|
if (session instanceof Response) return session;
|
||||||
|
const ids = idList(session.body, 'contact[]');
|
||||||
|
if (!ids) return showContacts(c, services, { removeError: 'That saved contact is not available.', status: 400 });
|
||||||
|
try {
|
||||||
|
const removed = removeContacts(services.db, session.coordinatorId, ids, services.now());
|
||||||
|
if (!removed.ok) return showContacts(c, services, { removeError: 'That saved contact is not available.', status: 400 });
|
||||||
|
return c.redirect(appPath(services.config, `/admin/contacts?removed=${removed.count}`), 303);
|
||||||
|
} catch (err) {
|
||||||
|
return saveFailed(c, services, session, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function csvDownload(c: Context, filename: string, csv: string): Response {
|
function csvDownload(c: Context, filename: string, csv: string): Response {
|
||||||
return c.body(new TextEncoder().encode(csv), 200, {
|
return c.body(new TextEncoder().encode(csv), 200, {
|
||||||
'Cache-Control': 'no-store',
|
'Cache-Control': 'no-store',
|
||||||
@@ -1788,6 +1807,13 @@ function wholeNumber(value: string | undefined): number | null {
|
|||||||
return Number.isSafeInteger(parsed) ? parsed : null;
|
return Number.isSafeInteger(parsed) ? parsed : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Notice text after a bulk removal. Blank unless the count is a real positive number. */
|
||||||
|
function contactsRemovedNotice(value: string | undefined): string | undefined {
|
||||||
|
const count = wholeNumber(value);
|
||||||
|
if (count === null || count === 0) return undefined;
|
||||||
|
return count === 1 ? '1 contact removed.' : `${count} contacts removed.`;
|
||||||
|
}
|
||||||
|
|
||||||
function importLocation(config: AppConfig, result: ContactImportSummary): string {
|
function importLocation(config: AppConfig, result: ContactImportSummary): string {
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
notice: 'contacts-imported',
|
notice: 'contacts-imported',
|
||||||
|
|||||||
+38
-4
@@ -47,6 +47,8 @@ const SHIFT_WAIT_BODY = 'Cancel this shift, or wait until it has ended, before r
|
|||||||
const SHIFT_WEEKLY_NOTE =
|
const SHIFT_WEEKLY_NOTE =
|
||||||
'This date came from a weekly template that is still active. Saving that template puts this date back. End that template first if the date should stay gone.';
|
'This date came from a weekly template that is still active. Saving that template puts this date back. End that template first if the date should stay gone.';
|
||||||
const PERSON_DELETE_BODY = 'This deletes their name, email, phone, and notes from the roster. It does not send email.';
|
const PERSON_DELETE_BODY = 'This deletes their name, email, phone, and notes from the roster. It does not send email.';
|
||||||
|
const CONTACT_REMOVE_MANY_BODY =
|
||||||
|
'This deletes their names, emails, and phones from the contact list. Their signups stay, and no email is sent.';
|
||||||
const PERSON_WAIT_BODY = 'Cancel this signup before removing it.';
|
const PERSON_WAIT_BODY = 'Cancel this signup before removing it.';
|
||||||
const PERSON_CANCEL_BODY = 'Cancelling a signup emails them. Their old private link stops working, and the email includes a new one.';
|
const PERSON_CANCEL_BODY = 'Cancelling a signup emails them. Their old private link stops working, and the email includes a new one.';
|
||||||
const SHIFT_CANCEL_EMPTY = 'No one is signed up. Cancelling closes the shift, takes it off this list, and does not send email.';
|
const SHIFT_CANCEL_EMPTY = 'No one is signed up. Cancelling closes the shift, takes it off this list, and does not send email.';
|
||||||
@@ -624,6 +626,8 @@ export function ContactsPage(props: {
|
|||||||
query: string;
|
query: string;
|
||||||
limit: number;
|
limit: number;
|
||||||
notice?: string;
|
notice?: string;
|
||||||
|
noticeText?: string;
|
||||||
|
removeError?: string;
|
||||||
summary?: ContactImportSummary;
|
summary?: ContactImportSummary;
|
||||||
importError?: string;
|
importError?: string;
|
||||||
manual?: { values: ContactManualValues; errors: ContactManualErrors };
|
manual?: { values: ContactManualValues; errors: ContactManualErrors };
|
||||||
@@ -645,7 +649,7 @@ export function ContactsPage(props: {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Contacts</h1>
|
<h1>Contacts</h1>
|
||||||
{props.summary ? <Notice text={contactImportSentence(props.summary)} /> : <Notice notice={props.notice} />}
|
{props.summary ? <Notice text={contactImportSentence(props.summary)} /> : <Notice text={props.noticeText} notice={props.notice} />}
|
||||||
{props.summary && props.summary.skips.length > 0 ? (
|
{props.summary && props.summary.skips.length > 0 ? (
|
||||||
<ul>
|
<ul>
|
||||||
{props.summary.skips.map((skip) => (
|
{props.summary.skips.map((skip) => (
|
||||||
@@ -654,7 +658,8 @@ export function ContactsPage(props: {
|
|||||||
</ul>
|
</ul>
|
||||||
) : null}
|
) : null}
|
||||||
{hiddenSkips > 0 ? <p class="panel-note">{hiddenSkips === 1 ? '1 more row was skipped.' : `${hiddenSkips} more rows were skipped.`}</p> : null}
|
{hiddenSkips > 0 ? <p class="panel-note">{hiddenSkips === 1 ? '1 more row was skipped.' : `${hiddenSkips} more rows were skipped.`}</p> : null}
|
||||||
<section class="desk-events" aria-labelledby="contacts-heading">
|
{props.removeError ? <p class="error">{props.removeError}</p> : null}
|
||||||
|
<section class="desk-panel contacts-board" aria-labelledby="contacts-heading">
|
||||||
<h2 id="contacts-heading">{heading}</h2>
|
<h2 id="contacts-heading">{heading}</h2>
|
||||||
<div class="contact-toolbar">
|
<div class="contact-toolbar">
|
||||||
{props.total > 0 ? (
|
{props.total > 0 ? (
|
||||||
@@ -717,11 +722,34 @@ export function ContactsPage(props: {
|
|||||||
</details>
|
</details>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{props.people.length > 0 ? (
|
||||||
|
<div class="desk-actions pick-bar">
|
||||||
|
{props.people.length > 1 ? (
|
||||||
|
<label class="pick-all">
|
||||||
|
<input type="checkbox" data-pick-all="contact" />
|
||||||
|
Select all
|
||||||
|
</label>
|
||||||
|
) : null}
|
||||||
|
<MassRemove
|
||||||
|
pick="contact"
|
||||||
|
unit="contact"
|
||||||
|
action={appPath(props.config, '/admin/contacts/remove')}
|
||||||
|
title="Remove selected contacts"
|
||||||
|
body={CONTACT_REMOVE_MANY_BODY}
|
||||||
|
submit="Remove selected"
|
||||||
|
>
|
||||||
|
Remove selected
|
||||||
|
</MassRemove>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
{props.total > 0 && props.people.length === 0 ? <p class="empty">No contacts match.</p> : null}
|
{props.total > 0 && props.people.length === 0 ? <p class="empty">No contacts match.</p> : null}
|
||||||
{props.people.length > 0 ? (
|
{props.people.length > 0 ? (
|
||||||
<ul class="contact-list" id="contacts-list">
|
<ul class="contact-list" id="contacts-list">
|
||||||
{props.people.map((person, index) => (
|
{props.people.map((person, index) => (
|
||||||
<li class="contact-row" data-contact-row="1" hidden={index >= props.limit ? true : undefined} key={person.id}>
|
<li class="contact-row" data-contact-row="1" hidden={index >= props.limit ? true : undefined} key={person.id}>
|
||||||
|
<label class="pick">
|
||||||
|
<input type="checkbox" data-pick="contact" value={String(person.id)} aria-label={`Select ${person.name}`} />
|
||||||
|
</label>
|
||||||
<p class="contact-name">
|
<p class="contact-name">
|
||||||
<strong>
|
<strong>
|
||||||
<a href={appPath(props.config, `/admin/contacts/${person.id}`)}>{person.name}</a>
|
<a href={appPath(props.config, `/admin/contacts/${person.id}`)}>{person.name}</a>
|
||||||
@@ -730,7 +758,11 @@ export function ContactsPage(props: {
|
|||||||
<p class="contact-email">{person.email}</p>
|
<p class="contact-email">{person.email}</p>
|
||||||
<p class="contact-phone">{person.phone ?? ''}</p>
|
<p class="contact-phone">{person.phone ?? ''}</p>
|
||||||
<p class="contact-remove">
|
<p class="contact-remove">
|
||||||
<ConfirmAnchor href={appPath(props.config, `/admin/contacts/${person.id}/remove`)} prompt={contactRemovalPrompt(person)}>
|
<ConfirmAnchor
|
||||||
|
class="text-action"
|
||||||
|
href={appPath(props.config, `/admin/contacts/${person.id}/remove`)}
|
||||||
|
prompt={contactRemovalPrompt(person)}
|
||||||
|
>
|
||||||
Remove
|
Remove
|
||||||
</ConfirmAnchor>
|
</ConfirmAnchor>
|
||||||
</p>
|
</p>
|
||||||
@@ -2142,12 +2174,13 @@ function Notice(props: { notice?: string; text?: string }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function MassRemove(props: {
|
function MassRemove(props: {
|
||||||
pick: 'shift' | 'signup';
|
pick: 'shift' | 'signup' | 'contact';
|
||||||
action: string;
|
action: string;
|
||||||
title: string;
|
title: string;
|
||||||
body: string;
|
body: string;
|
||||||
submit: string;
|
submit: string;
|
||||||
note?: string;
|
note?: string;
|
||||||
|
unit?: string;
|
||||||
gate?: 'open' | 'removable';
|
gate?: 'open' | 'removable';
|
||||||
verb?: string;
|
verb?: string;
|
||||||
blocked?: string;
|
blocked?: string;
|
||||||
@@ -2169,6 +2202,7 @@ function MassRemove(props: {
|
|||||||
data-confirm-title={props.title}
|
data-confirm-title={props.title}
|
||||||
data-confirm-body={props.body}
|
data-confirm-body={props.body}
|
||||||
data-confirm-note-weekly={props.note}
|
data-confirm-note-weekly={props.note}
|
||||||
|
data-confirm-unit={props.unit}
|
||||||
data-confirm-submit={props.submit}
|
data-confirm-submit={props.submit}
|
||||||
>
|
>
|
||||||
{props.children}
|
{props.children}
|
||||||
|
|||||||
+3
-1
@@ -1,11 +1,13 @@
|
|||||||
import type Database from 'better-sqlite3';
|
import type Database from 'better-sqlite3';
|
||||||
|
|
||||||
/** Records an admin or host action. Detail is ids and counts only, never addresses or notes. */
|
/** Records an admin or host action. Detail is ids and counts only, never addresses or notes. */
|
||||||
|
export type AuditDetail = Record<string, number | number[]>;
|
||||||
|
|
||||||
export function writeAudit(
|
export function writeAudit(
|
||||||
db: Database.Database,
|
db: Database.Database,
|
||||||
coordinatorId: number | null,
|
coordinatorId: number | null,
|
||||||
action: string,
|
action: string,
|
||||||
detail: Record<string, number>,
|
detail: AuditDetail,
|
||||||
now: string,
|
now: string,
|
||||||
): void {
|
): void {
|
||||||
const json = JSON.stringify(detail);
|
const json = JSON.stringify(detail);
|
||||||
|
|||||||
@@ -264,6 +264,30 @@ export function removeContact(db: Database.Database, coordinatorId: number, id:
|
|||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes several contacts at once, in one transaction.
|
||||||
|
* If any id is missing, nothing is removed. Signups are never touched.
|
||||||
|
*/
|
||||||
|
export function removeContacts(
|
||||||
|
db: Database.Database,
|
||||||
|
coordinatorId: number,
|
||||||
|
ids: number[],
|
||||||
|
now: string,
|
||||||
|
): { ok: true; count: number } | { ok: false } {
|
||||||
|
if (ids.length === 0) return { ok: true, count: 0 };
|
||||||
|
return db.transaction(() => {
|
||||||
|
const find = db.prepare('SELECT id FROM contacts WHERE id = ?');
|
||||||
|
for (const id of ids) {
|
||||||
|
if (!find.get(id)) return { ok: false } as const;
|
||||||
|
}
|
||||||
|
const del = db.prepare('DELETE FROM contacts WHERE id = ?');
|
||||||
|
let count = 0;
|
||||||
|
for (const id of ids) count += del.run(id).changes;
|
||||||
|
writeAudit(db, coordinatorId, 'contacts.remove', { count, ids }, now);
|
||||||
|
return { ok: true, count } as const;
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves the person from a confirmed signup.
|
* Saves the person from a confirmed signup.
|
||||||
* The same name and email updates that contact. A different name is a new contact, even with the same email.
|
* The same name and email updates that contact. A different name is a new contact, even with the same email.
|
||||||
|
|||||||
@@ -799,6 +799,117 @@ describe('contact pages', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('bulk contact removal', () => {
|
||||||
|
function insertContact(
|
||||||
|
db: Awaited<ReturnType<typeof openDb>>,
|
||||||
|
person: { first: string; last: string; email: string; phone?: string | null },
|
||||||
|
): number {
|
||||||
|
return Number(
|
||||||
|
db.prepare(
|
||||||
|
`INSERT INTO contacts (first_name, last_name, email, email_norm, phone, created_at, updated_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||||
|
).run(person.first, person.last, person.email, person.email.toLowerCase(), person.phone ?? null, NOW, NOW).lastInsertRowid,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function postBulk(
|
||||||
|
app: Awaited<ReturnType<typeof buildApp>>['app'],
|
||||||
|
cookie: string | undefined,
|
||||||
|
ids: number[],
|
||||||
|
origin = ORIGIN,
|
||||||
|
) {
|
||||||
|
const current = await app.request('/admin/contacts', { headers: cookie ? { cookie } : {} });
|
||||||
|
const token = /name="csrf" value="([^"]*)"/.exec(await current.text())?.[1] ?? '';
|
||||||
|
const body = new URLSearchParams({ csrf: token });
|
||||||
|
for (const id of ids) body.append('contact[]', String(id));
|
||||||
|
return app.request('/admin/contacts/remove', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { origin, 'content-type': 'application/x-www-form-urlencoded', ...(cookie ? { cookie } : {}) },
|
||||||
|
body,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
it('shows a checkbox per contact with Select all and Remove selected', async () => {
|
||||||
|
const { app, db } = await buildApp();
|
||||||
|
const cookie = await signIn(app, db);
|
||||||
|
insertContact(db, { first: 'Ada', last: 'One', email: 'ada@example.invalid' });
|
||||||
|
insertContact(db, { first: 'Bea', last: 'Two', email: 'bea@example.invalid', phone: '712-555-0100' });
|
||||||
|
const page = await (await app.request('/admin/contacts', { headers: { cookie } })).text();
|
||||||
|
expect(page).toContain('data-pick-all="contact"');
|
||||||
|
expect(page).toContain('Select all');
|
||||||
|
expect(page).toContain('Remove selected');
|
||||||
|
expect(page).toContain('data-confirm-pick="contact"');
|
||||||
|
expect(page).toContain('title="Remove selected contacts"');
|
||||||
|
expect(page).toContain('data-pick="contact" value="1"');
|
||||||
|
expect(page).toContain('data-pick="contact" value="2"');
|
||||||
|
expect(page).toContain('aria-label="Select Ada One"');
|
||||||
|
expect(page).toContain('aria-label="Select Bea Two"');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes exactly the chosen contacts, keeps their signups, and writes one audit row', async () => {
|
||||||
|
const { app, db } = await buildApp();
|
||||||
|
const cookie = await signIn(app, db);
|
||||||
|
const adaId = insertContact(db, { first: 'Ada', last: 'One', email: 'ada@example.invalid' });
|
||||||
|
const beaId = insertContact(db, { first: 'Bea', last: 'Two', email: 'bea@example.invalid' });
|
||||||
|
const camId = insertContact(db, { first: 'Cam', last: 'Three', email: 'cam@example.invalid' });
|
||||||
|
const shiftId = insertShift(db);
|
||||||
|
insertSignup(db, shiftId, { name: 'Ada One', email: 'ada@example.invalid' });
|
||||||
|
|
||||||
|
const removed = await postBulk(app, cookie, [adaId, beaId]);
|
||||||
|
expect(removed.status).toBe(303);
|
||||||
|
expect(removed.headers.get('location')).toBe('/admin/contacts?removed=2');
|
||||||
|
const page = await (await app.request(removed.headers.get('location') ?? '', { headers: { cookie } })).text();
|
||||||
|
expect(page).toContain('2 contacts removed.');
|
||||||
|
expect(count(db, 'SELECT COUNT(*) AS n FROM contacts')).toBe(1);
|
||||||
|
expect(db.prepare('SELECT email FROM contacts WHERE id = ?').get(camId)).toEqual({ email: 'cam@example.invalid' });
|
||||||
|
expect(
|
||||||
|
count(db, "SELECT COUNT(*) AS n FROM signups WHERE email_norm = 'ada@example.invalid' AND status = 'confirmed'"),
|
||||||
|
).toBe(1);
|
||||||
|
const audits = db.prepare(`SELECT detail_json AS detail_json FROM audit WHERE action = 'contacts.remove'`).all() as {
|
||||||
|
detail_json: string;
|
||||||
|
}[];
|
||||||
|
const audit = audits[0];
|
||||||
|
expect(audits).toHaveLength(1);
|
||||||
|
if (!audit) return;
|
||||||
|
expect(JSON.parse(audit.detail_json)).toEqual({ count: 2, ids: [adaId, beaId] });
|
||||||
|
expect(audit.detail_json).not.toContain('@');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shows one removal notice for a single contact', async () => {
|
||||||
|
const { app, db } = await buildApp();
|
||||||
|
const cookie = await signIn(app, db);
|
||||||
|
const adaId = insertContact(db, { first: 'Ada', last: 'One', email: 'ada@example.invalid' });
|
||||||
|
const removed = await postBulk(app, cookie, [adaId]);
|
||||||
|
expect(removed.status).toBe(303);
|
||||||
|
expect(removed.headers.get('location')).toBe('/admin/contacts?removed=1');
|
||||||
|
const page = await (await app.request(removed.headers.get('location') ?? '', { headers: { cookie } })).text();
|
||||||
|
expect(page).toContain('1 contact removed.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes nothing when one chosen contact is missing', async () => {
|
||||||
|
const { app, db } = await buildApp();
|
||||||
|
const cookie = await signIn(app, db);
|
||||||
|
const adaId = insertContact(db, { first: 'Ada', last: 'One', email: 'ada@example.invalid' });
|
||||||
|
const removed = await postBulk(app, cookie, [adaId, 9999]);
|
||||||
|
expect(removed.status).toBe(400);
|
||||||
|
expect(await removed.text()).toContain('That saved contact is not available.');
|
||||||
|
expect(count(db, 'SELECT COUNT(*) AS n FROM contacts')).toBe(1);
|
||||||
|
expect(count(db, `SELECT COUNT(*) AS n FROM audit WHERE action = 'contacts.remove'`)).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('asks a signed-out visitor to sign in and rejects a foreign origin', async () => {
|
||||||
|
const { app, db } = await buildApp();
|
||||||
|
const loggedOut = await postBulk(app, undefined, [1]);
|
||||||
|
expect(loggedOut.status).toBe(303);
|
||||||
|
expect(loggedOut.headers.get('location')).toBe('/login');
|
||||||
|
const cookie = await signIn(app, db);
|
||||||
|
insertContact(db, { first: 'Ada', last: 'One', email: 'ada@example.invalid' });
|
||||||
|
const foreign = await postBulk(app, cookie, [1], 'https://elsewhere.example');
|
||||||
|
expect(foreign.status).toBe(403);
|
||||||
|
expect(count(db, 'SELECT COUNT(*) AS n FROM contacts')).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
async function openDb() {
|
async function openDb() {
|
||||||
const root = mkdtempSync(path.join(tmpdir(), 'hope-contacts-'));
|
const root = mkdtempSync(path.join(tmpdir(), 'hope-contacts-'));
|
||||||
dirs.push(root);
|
dirs.push(root);
|
||||||
|
|||||||
Reference in new issue
Block a user