From deb7d92367bd96da4b4e65e68c81e34dfcf56517 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 11 Oct 2026 09:20:31 -0400 Subject: [PATCH] fix: Persist every previous team subdomain (#14022) The savePreviousSubdomain hook mutated the existing array in place, so Sequelize did not detect the change and only the first previous subdomain was ever written to the database. --- server/models/Team.test.ts | 15 +++++++++++++++ server/models/Team.ts | 16 ++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/server/models/Team.test.ts b/server/models/Team.test.ts index 10e55b25bc..84d95c5c6c 100644 --- a/server/models/Team.test.ts +++ b/server/models/Team.test.ts @@ -118,6 +118,21 @@ describe("Team", () => { expect(team.previousSubdomains?.[0]).toEqual(originalSubdomain); expect(team.previousSubdomains?.[1]).toEqual(subdomain); }); + + it("should persist every previous subdomain", async () => { + const id = randomUUID(); + const subdomains = [1, 2, 3, 4, 5].map((n) => `sub${n}-${id}`); + const team = await buildTeam({ subdomain: subdomains[0] }); + + for (const subdomain of subdomains.slice(1)) { + await team.update({ subdomain }); + } + + await team.reload(); + expect(team.previousSubdomains).toEqual(subdomains.slice(1, 4)); + expect((await Team.findBySubdomain(subdomains[3]))?.id).toEqual(team.id); + expect(await Team.findBySubdomain(subdomains[0])).toBeNull(); + }); }); describe("publicAvatarUrl", () => { diff --git a/server/models/Team.ts b/server/models/Team.ts index 670b46009e..5e2f0e4881 100644 --- a/server/models/Team.ts +++ b/server/models/Team.ts @@ -536,15 +536,15 @@ class Team extends ParanoidModel< static async savePreviousSubdomain(model: Team) { const previousSubdomain = model.previous("subdomain"); if (previousSubdomain && previousSubdomain !== model.subdomain) { - model.previousSubdomains = model.previousSubdomains || []; + const previousSubdomains = model.previousSubdomains || []; - if (!model.previousSubdomains.includes(previousSubdomain)) { - // Add the previous subdomain to the list of previous subdomains - // upto a maximum of 3 previous subdomains - model.previousSubdomains.push(previousSubdomain); - if (model.previousSubdomains.length > 3) { - model.previousSubdomains.shift(); - } + if (!previousSubdomains.includes(previousSubdomain)) { + // Assign a new array, Sequelize does not detect in-place mutation. + // Keep up to a maximum of 3 previous subdomains. + model.previousSubdomains = [ + ...previousSubdomains, + previousSubdomain, + ].slice(-3); } }