fix: enforce one active logical album grant
This commit is contained in:
1 parent
74ab2eb85f
commit
c400167b8f
9 files changed
+577
-45
No files matched your search
@@ -4,6 +4,8 @@ import {
|
||||
ALBUM_GRANT_LOCK_ORDER,
|
||||
AlbumGrantError,
|
||||
createAlbumGrantService,
|
||||
planAlbumGrantMutation,
|
||||
planAlbumRevokeMutation,
|
||||
type AlbumGrantRepository,
|
||||
} from "./grants";
|
||||
import { EDITORIAL_MUTATION_LOCK_ORDER } from "@/modules/editorials/service";
|
||||
@@ -23,6 +25,96 @@ const albumUuid = "2222222b-2222-4222-8222-22222222222b";
|
||||
const adminUuid = "3333333c-3333-4333-8333-33333333333c";
|
||||
const unavailableAlbumUuid = "4444444d-4444-4444-8444-44444444444d";
|
||||
|
||||
const earlierGrant = new Date("2026-08-07T08:00:00.000Z");
|
||||
const laterGrant = new Date("2026-08-08T08:00:00.000Z");
|
||||
|
||||
test("direct grant keeps the exact canonical row and retires every active case variant", () => {
|
||||
expect(
|
||||
planAlbumGrantMutation(
|
||||
[
|
||||
{
|
||||
id: "00000000-0000-4000-8000-000000000001",
|
||||
immichAlbumId: albumUuid.toUpperCase(),
|
||||
grantedAt: earlierGrant,
|
||||
revokedAt: null,
|
||||
},
|
||||
{
|
||||
id: "00000000-0000-4000-8000-000000000002",
|
||||
immichAlbumId: albumUuid,
|
||||
grantedAt: laterGrant,
|
||||
revokedAt: null,
|
||||
},
|
||||
],
|
||||
albumUuid,
|
||||
),
|
||||
).toEqual({
|
||||
survivorId: "00000000-0000-4000-8000-000000000002",
|
||||
activeLoserIds: ["00000000-0000-4000-8000-000000000001"],
|
||||
hadActiveGrant: true,
|
||||
});
|
||||
});
|
||||
|
||||
test("regrant chooses the oldest revoked case variant then its stable id", () => {
|
||||
const revokedAt = new Date("2026-08-09T08:00:00.000Z");
|
||||
expect(
|
||||
planAlbumGrantMutation(
|
||||
[
|
||||
{
|
||||
id: "00000000-0000-4000-8000-000000000003",
|
||||
immichAlbumId: albumUuid.toUpperCase(),
|
||||
grantedAt: laterGrant,
|
||||
revokedAt,
|
||||
},
|
||||
{
|
||||
id: "00000000-0000-4000-8000-000000000002",
|
||||
immichAlbumId: "2222222B-2222-4222-8222-22222222222b",
|
||||
grantedAt: earlierGrant,
|
||||
revokedAt,
|
||||
},
|
||||
{
|
||||
id: "00000000-0000-4000-8000-000000000001",
|
||||
immichAlbumId: "2222222b-2222-4222-8222-22222222222B",
|
||||
grantedAt: earlierGrant,
|
||||
revokedAt,
|
||||
},
|
||||
],
|
||||
albumUuid,
|
||||
),
|
||||
).toEqual({
|
||||
survivorId: "00000000-0000-4000-8000-000000000001",
|
||||
activeLoserIds: [],
|
||||
hadActiveGrant: false,
|
||||
});
|
||||
});
|
||||
|
||||
test("revoke targets every active case-equivalent row", () => {
|
||||
expect(
|
||||
planAlbumRevokeMutation([
|
||||
{
|
||||
id: "00000000-0000-4000-8000-000000000003",
|
||||
immichAlbumId: albumUuid.toUpperCase(),
|
||||
grantedAt: laterGrant,
|
||||
revokedAt: null,
|
||||
},
|
||||
{
|
||||
id: "00000000-0000-4000-8000-000000000001",
|
||||
immichAlbumId: albumUuid,
|
||||
grantedAt: earlierGrant,
|
||||
revokedAt: null,
|
||||
},
|
||||
{
|
||||
id: "00000000-0000-4000-8000-000000000002",
|
||||
immichAlbumId: albumUuid,
|
||||
grantedAt: earlierGrant,
|
||||
revokedAt: new Date("2026-08-09T08:00:00.000Z"),
|
||||
},
|
||||
]),
|
||||
).toEqual([
|
||||
"00000000-0000-4000-8000-000000000001",
|
||||
"00000000-0000-4000-8000-000000000003",
|
||||
]);
|
||||
});
|
||||
|
||||
function createRepository(initial: Grant[] = []) {
|
||||
const grants = [...initial];
|
||||
const audit: Audit[] = [];
|
||||
@@ -33,26 +125,28 @@ function createRepository(initial: Grant[] = []) {
|
||||
.map((grant) => grant.albumId);
|
||||
},
|
||||
async grant(input) {
|
||||
const active = grants.filter(
|
||||
const matching = grants.filter(
|
||||
(grant) =>
|
||||
grant.editorialId === input.editorialId &&
|
||||
grant.albumId.toLowerCase() === input.immichAlbumId &&
|
||||
grant.revokedAt === null,
|
||||
grant.albumId.toLowerCase() === input.immichAlbumId,
|
||||
);
|
||||
const survivor =
|
||||
matching.find((grant) => grant.albumId === input.immichAlbumId) ?? matching[0];
|
||||
const active = matching.filter((grant) => grant.revokedAt === null);
|
||||
if (active.length > 0) {
|
||||
for (const grant of active) grant.albumId = input.immichAlbumId;
|
||||
for (const grant of active) {
|
||||
if (grant !== survivor) grant.revokedAt = new Date();
|
||||
}
|
||||
if (survivor) {
|
||||
survivor.albumId = input.immichAlbumId;
|
||||
survivor.revokedAt = null;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const revoked = grants.find(
|
||||
(grant) =>
|
||||
grant.editorialId === input.editorialId &&
|
||||
grant.albumId.toLowerCase() === input.immichAlbumId &&
|
||||
grant.revokedAt !== null,
|
||||
);
|
||||
if (revoked) {
|
||||
revoked.albumId = input.immichAlbumId;
|
||||
revoked.revokedAt = null;
|
||||
if (survivor) {
|
||||
survivor.albumId = input.immichAlbumId;
|
||||
survivor.revokedAt = null;
|
||||
} else {
|
||||
grants.push({ editorialId: input.editorialId, albumId: input.immichAlbumId, revokedAt: null });
|
||||
}
|
||||
@@ -75,7 +169,6 @@ function createRepository(initial: Grant[] = []) {
|
||||
);
|
||||
if (active.length === 0) return false;
|
||||
for (const grant of active) {
|
||||
grant.albumId = input.immichAlbumId;
|
||||
grant.revokedAt = new Date();
|
||||
}
|
||||
audit.push({
|
||||
@@ -165,6 +258,37 @@ test("treats an uppercase historical album grant as the canonical active grant",
|
||||
expect(audit).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("returns one canonical active ID when historical rows contain case duplicates", async () => {
|
||||
const { service } = createService([
|
||||
{ editorialId: editorialUuid, albumId: albumUuid.toUpperCase(), revokedAt: null },
|
||||
{ editorialId: editorialUuid, albumId: albumUuid, revokedAt: null },
|
||||
]);
|
||||
|
||||
await expect(service.getGrantedAlbumIds(editorialUuid)).resolves.toEqual([albumUuid]);
|
||||
});
|
||||
|
||||
test("direct grant consolidates active case variants without a second logical audit", async () => {
|
||||
const { service, grants, audit } = createService([
|
||||
{ editorialId: editorialUuid, albumId: albumUuid.toUpperCase(), revokedAt: null },
|
||||
{ editorialId: editorialUuid, albumId: albumUuid, revokedAt: null },
|
||||
]);
|
||||
|
||||
await service.grantAlbum({
|
||||
editorialId: editorialUuid,
|
||||
immichAlbumId: albumUuid,
|
||||
adminUserId: adminUuid,
|
||||
});
|
||||
await service.grantAlbum({
|
||||
editorialId: editorialUuid,
|
||||
immichAlbumId: albumUuid,
|
||||
adminUserId: adminUuid,
|
||||
});
|
||||
|
||||
expect(grants.filter((grant) => grant.revokedAt === null)).toHaveLength(1);
|
||||
await expect(service.getGrantedAlbumIds(editorialUuid)).resolves.toEqual([albumUuid]);
|
||||
expect(audit).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("revokes and regrants case-duplicate historical rows as one canonical grant", async () => {
|
||||
const { service, grants, audit } = createService([
|
||||
{ editorialId: editorialUuid, albumId: albumUuid.toUpperCase(), revokedAt: null },
|
||||
@@ -177,7 +301,6 @@ test("revokes and regrants case-duplicate historical rows as one canonical grant
|
||||
adminUserId: adminUuid,
|
||||
});
|
||||
expect(grants.filter((grant) => grant.revokedAt === null)).toHaveLength(0);
|
||||
expect(grants.every((grant) => grant.albumId === albumUuid)).toBe(true);
|
||||
|
||||
await service.grantAlbum({
|
||||
editorialId: editorialUuid,
|
||||
|
||||
Reference in new issue
Block a user