Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(mail-ingest): make the own address once when two views ask for it together

Maurice403b7d2f9368
diff --git a/server/src/nest/mail-ingest/mail-ingest-address.service.ts b/server/src/nest/mail-ingest/mail-ingest-address.service.ts
index 974ab39a2..44c359ccb 100644
--- a/server/src/nest/mail-ingest/mail-ingest-address.service.ts
+++ b/server/src/nest/mail-ingest/mail-ingest-address.service.ts
@@ -98,7 +98,7 @@ export class MailIngestAddressService {
/** The person's own live address, made now when they have none: an uploaded mail goes through it. */
async ensureOwn(userId: number): Promise<MailIngestAddressRow> {
- return (await this.addresses.findLive(userId, null)) ?? (await this.rotate(userId, null));
+ return (await this.addresses.findLive(userId, null)) ?? (await this.issue(userId, null, false));
}
/** Whether an address takes mail: not revoked, its person's import on, a trip address's owner still an editor. */
@@ -119,9 +119,20 @@ export class MailIngestAddressService {
await this.addresses.touchUsed(addressId, dbNow());
}
- private async rotate(userId: number, tripId: number | null): Promise<MailIngestAddressRow> {
+ private rotate(userId: number, tripId: number | null): Promise<MailIngestAddressRow> {
+ return this.issue(userId, tripId, true);
+ }
+
+ /**
+ * A new address for the person, replacing the live one, or with `replace`
+ * off only when there is none yet: two first looks at once then agree on
+ * one address instead of the second revoking the one the first showed.
+ */
+ private async issue(userId: number, tripId: number | null, replace: boolean): Promise<MailIngestAddressRow> {
const token = newToken();
const id = await this.uow.transactional(async () => {
+ const live = replace ? null : await this.addresses.findLive(userId, tripId);
+ if (live) return live.id;
const now = dbNow();
await this.addresses.revokeLive(userId, tripId, now);
return this.addresses.insertAddress({
diff --git a/server/tests/unit/nest/mail-ingest/mail-ingest.addresses.test.ts b/server/tests/unit/nest/mail-ingest/mail-ingest.addresses.test.ts
index 77bb86568..1c252e8ba 100644
--- a/server/tests/unit/nest/mail-ingest/mail-ingest.addresses.test.ts
+++ b/server/tests/unit/nest/mail-ingest/mail-ingest.addresses.test.ts
@@ -15,7 +15,7 @@ import { MailIngestSettingsService } from '../../../../src/nest/mail-ingest/mail
import { MailIngestModule } from '../../../../src/nest/mail-ingest/mail-ingest.module';
import { PermissionsService } from '../../../../src/nest/permissions/permissions.service';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
-import { deleteRows, findRow } from '../../../helpers/factories/rows';
+import { countRows, deleteRows, findRow } from '../../../helpers/factories/rows';
import { setAppSetting } from '../../../helpers/factories/settings';
import { addTripMember, makeTrip } from '../../../helpers/factories/trips';
import { makeUser } from '../../../helpers/factories/users';
@@ -87,6 +87,18 @@ describe('a person’s own address', () => {
expect(await addresses().ownAddress(user.id)).toBeNull();
});
+ it('is made once when two views look at it first at the same time, and both show the one that works', async () => {
+ const { user } = await makeUser(orm());
+ const [a, b] = await Promise.all([
+ addresses().ownAddressForCard(user.id, true),
+ addresses().ownAddressForCard(user.id, true),
+ ]);
+ expect(a?.address).toBeTruthy();
+ expect(b?.address).toBe(a?.address);
+ expect(await countRows(orm(), MailIngestAddresses, { user_id: user.id })).toBe(1);
+ expect((await addresses().resolveAll([a?.address ?? '']))[0]?.user_id).toBe(user.id);
+ });
+
it('takes no mail while its person has the import off', async () => {
const { user } = await makeUser(orm());
const own = await addresses().renewOwn(user.id);