fix(notifications): list the paddle region request for admins while any trip has Tours on

The admin matrix decided a trip addon by the admin's own trips, so with Tours
off for the instance and on for a member's trip the paddle region request still
reached admins but had no row to switch it. Admin-scoped rows now follow the
addon being on for the instance or any trip.
This commit is contained in:
Maurice
2026-11-23 15:10:41 +01:00
parent 146f46c5e3
commit c289541c43
7 changed files with 79 additions and 7 deletions
@@ -95,6 +95,18 @@ export class TripAddonOverridesRepository extends TrekRepository<TripAddonOverri
.execute();
}
/** Whether any trip at all switched the addon on for itself. */
async anyEnabled(addon_id: string): Promise<boolean> {
const row = await this.kysely<OverridesKyselyDB>()
.selectFrom('trip_addon_overrides')
.select('trip_id')
.where('addon_id', '=', addon_id)
.where('enabled', '=', 1)
.limit(1)
.executeTakeFirst();
return row !== undefined;
}
/**
* Whether any trip the user owns or is a member of switched the addon on for
* itself. Routes without a trip (a library, a search across trips) open on
+9
View File
@@ -110,6 +110,15 @@ export class AddonsService {
return this.tripOverrides.anyEnabledForUser(userId, addon.id);
}
/** For what the whole install can raise: on for the instance, or for any trip that switched it on. */
async isEnabledAnywhere(addonId: string): Promise<boolean> {
const addon = await this.addons.findById(addonId);
if (!addon) return false;
if (addon.enabled) return true;
if (!isTripAddonRow(addon)) return false;
return this.tripOverrides.anyEnabled(addon.id);
}
async getBagTracking() {
const value = await readAppSetting(this.appSettings, 'bag_tracking_enabled');
return { enabled: value === 'true' };
@@ -97,13 +97,15 @@ export class NotificationPreferencesService {
* the provider, is a switch that never does anything. For one user's own
* matrix a trip addon counts as on while it is on in any of their trips, so a
* trip that switched Collab on lists its chat row although the instance has it off.
* For the admin's matrix it counts as on while it is on in any trip at all,
* since an admin-scoped event fires from whichever trip has it.
*/
private async listedEventTypes(userId?: number): Promise<NotifEventType[]> {
private async listedEventTypes(userId?: number, scope: 'user' | 'admin' = 'user'): Promise<NotifEventType[]> {
const photoProviders = await this.addons.enabledPhotoProviderIds();
const read = (id: string): Promise<boolean> =>
userId !== undefined && TRIP_ADDON_IDS.has(id)
? this.addons.isEnabledForUserAnywhere(userId, id)
: this.addons.isAddonEnabled(id);
const read = (id: string): Promise<boolean> => {
if (userId === undefined || !TRIP_ADDON_IDS.has(id)) return this.addons.isAddonEnabled(id);
return scope === 'admin' ? this.addons.isEnabledAnywhere(id) : this.addons.isEnabledForUserAnywhere(userId, id);
};
// One read per addon, shared by the events that hang off it.
const addonOn = new Map<string, Promise<boolean>>();
const on = (id: string): Promise<boolean> => {
@@ -381,7 +383,7 @@ export class NotificationPreferencesService {
// Filter event types by scope
const event_types =
scope === 'admin'
? (await this.listedEventTypes(userId)).filter((e) => ADMIN_SCOPED_EVENTS.has(e))
? (await this.listedEventTypes(userId, 'admin')).filter((e) => ADMIN_SCOPED_EVENTS.has(e))
: (await this.listedEventTypes(userId)).filter((e) => !ADMIN_SCOPED_EVENTS.has(e));
return {
+6 -1
View File
@@ -111,13 +111,18 @@ export function everyFeatureOn(
overrides: Partial<
Pick<
AddonsService,
'isAddonEnabled' | 'isEnabledForUserAnywhere' | 'isWandererAvailable' | 'enabledPhotoProviderIds'
| 'isAddonEnabled'
| 'isEnabledForUserAnywhere'
| 'isEnabledAnywhere'
| 'isWandererAvailable'
| 'enabledPhotoProviderIds'
>
> = {},
): AddonsService {
return {
isAddonEnabled: async () => true,
isEnabledForUserAnywhere: async () => true,
isEnabledAnywhere: async () => true,
isWandererAvailable: async () => true,
enabledPhotoProviderIds: async () => ['immich', 'synologyphotos'],
...overrides,
@@ -130,4 +130,14 @@ describe('TripAddonOverridesRepository', () => {
await deleteRows(t, Trips, { id: trip.id });
expect(await countRows(t, TripAddonOverrides, { trip: trip.id })).toBe(0);
});
it('TAOREPO-010: anyEnabled sees any trip that switched the addon on, and none that switched it off', async () => {
const { trip } = await ownerAndTrip();
const { trip: other } = await ownerAndTrip();
await repo.upsertOverride(trip.id, 'tours', false);
expect(await repo.anyEnabled('tours')).toBe(false);
await repo.upsertOverride(other.id, 'tours', true);
expect(await repo.anyEnabled('tours')).toBe(true);
expect(await repo.anyEnabled('roadtrip')).toBe(false);
});
});
@@ -502,6 +502,27 @@ describe('listed events follow the features that are on', () => {
}
});
it('NPREF-LIST-004: the admin matrix lists the paddle region request while any trip has Tours on, not only on a trip of the admin', async () => {
const { user: admin } = createAdmin(testDb);
const all = { ...features };
Object.assign(features, {
isAddonEnabled: async (id: string) => id !== 'tours',
isEnabledForUserAnywhere: async () => false,
isEnabledAnywhere: async (id: string) => id === 'tours',
});
try {
expect((await svc.getPreferencesMatrix(admin.id, 'admin', 'admin')).event_types).toContain(
'paddle_region_requested',
);
Object.assign(features, { isEnabledAnywhere: async () => false });
expect((await svc.getPreferencesMatrix(admin.id, 'admin', 'admin')).event_types).not.toContain(
'paddle_region_requested',
);
} finally {
Object.assign(features, all);
}
});
it('NPREF-LIST-002: the flight events are listed only while live flight status is on', async () => {
const flights = ['flight_departed', 'flight_delayed', 'flight_landed'];
const { user } = createUser(testDb);
@@ -132,6 +132,19 @@ describe('TripAddonsService: routes without a trip', () => {
expect(await addons.isEnabledForUserAnywhere(owner.id, 'no_such_addon')).toBe(false);
});
it('TRIPADDON-SVC-015: isEnabledAnywhere opens for any trip that switched the addon on, whoever owns it', async () => {
const { owner, trip } = await ownedTrip();
await setAddonEnabled(orm(), 'tours', false);
expect(await addons.isEnabledAnywhere('tours')).toBe(false);
await svc.setTripOverride(trip.id, owner.id, 'tours', true);
expect(await addons.isEnabledAnywhere('tours')).toBe(true);
await setAddonEnabled(orm(), 'vacay', false);
expect(await addons.isEnabledAnywhere('vacay')).toBe(false);
expect(await addons.isEnabledAnywhere('no_such_addon')).toBe(false);
await setAddonEnabled(orm(), 'roadtrip', true);
expect(await addons.isEnabledAnywhere('roadtrip')).toBe(true);
});
it('TRIPADDON-SVC-013: tripsWithAddon keeps the trips the addon applies to, in their order, each as isEnabledForTrip says', async () => {
const { owner, trip: off } = await ownedTrip();
const on = await makeTrip(orm(), owner.id);