fix: use partial index for status page customDomain

Replace the sparse unique index with a partial index so multiple status
pages without a custom domain no longer collide on null values.
This commit is contained in:
Ian Troisi committed 2026-06-11 13:14:52 -04:00
1 parent 187c8eaba1
commit 0aef8b652f
3 files changed
+39 -9

No files matched your search

@@ -78,9 +78,10 @@ class MongoStatusPagesRepository implements IStatusPagesRepository {
create = async (userId: string, teamId: string, image: Express.Multer.File | undefined, data: Partial<StatusPage>): Promise<StatusPage> => {
const { logo, customDomain, ...restData } = data;
void logo;
const normalizedCustomDomain = normalizeStatusPageDomain(customDomain);
const statusPage = new StatusPageModel({
...restData,
customDomain: normalizeStatusPageDomain(customDomain),
...(normalizedCustomDomain ? { customDomain: normalizedCustomDomain } : {}),
userId,
teamId,
});
@@ -133,20 +134,35 @@ class MongoStatusPagesRepository implements IStatusPagesRepository {
): Promise<StatusPage> => {
const { logo, removeLogo, customDomain, ...restPatch } = patch;
void logo;
const updateData: StatusPageUpdateData = { ...restPatch };
const setData: StatusPageUpdateData = { ...restPatch };
const unsetData: Record<string, 1> = {};
if (customDomain !== undefined) {
updateData.customDomain = normalizeStatusPageDomain(customDomain);
const normalizedCustomDomain = normalizeStatusPageDomain(customDomain);
if (normalizedCustomDomain) {
setData.customDomain = normalizedCustomDomain;
} else {
unsetData.customDomain = 1;
}
}
if (image) {
updateData.logo = {
setData.logo = {
data: image.buffer as Buffer,
contentType: image.mimetype,
};
} else if (removeLogo === "true") {
updateData.logo = null;
setData.logo = null;
}
const statusPage = await StatusPageModel.findOneAndUpdate({ teamId, _id: id }, updateData, {
const updateQuery =
unsetData.customDomain !== undefined
? {
$set: setData,
$unset: unsetData,
}
: setData;
const statusPage = await StatusPageModel.findOneAndUpdate({ teamId, _id: id }, updateQuery, {
new: true,
});
@@ -67,11 +67,8 @@ const StatusPageSchema = new Schema<StatusPageDocument>(
},
customDomain: {
type: String,
default: null,
lowercase: true,
trim: true,
sparse: true,
unique: true,
},
timezone: {
type: String,
@@ -136,6 +133,14 @@ const StatusPageSchema = new Schema<StatusPageDocument>(
{ timestamps: true }
);
StatusPageSchema.index(
{ customDomain: 1 },
{
unique: true,
partialFilterExpression: { customDomain: { $type: "string" } },
}
);
const StatusPageModel = model<StatusPageDocument>("StatusPage", StatusPageSchema);
export type { StatusPageDocument };
@@ -134,6 +134,15 @@ describe("StatusPageService", () => {
expect(repo.create).toHaveBeenCalledWith("user-1", "team-1", undefined, expect.objectContaining({ customDomain: "status.example.com" }));
});
it("allows multiple status pages without a custom domain", async () => {
const { service, repo } = createService(true);
await service.createStatusPage("user-1", "team-1", undefined, { companyName: "First" });
await service.createStatusPage("user-1", "team-1", undefined, { companyName: "Second" });
expect(repo.create).toHaveBeenCalledTimes(2);
});
});
describe("getStatusPagesByTeamId", () => {