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:
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> => {
|
create = async (userId: string, teamId: string, image: Express.Multer.File | undefined, data: Partial<StatusPage>): Promise<StatusPage> => {
|
||||||
const { logo, customDomain, ...restData } = data;
|
const { logo, customDomain, ...restData } = data;
|
||||||
void logo;
|
void logo;
|
||||||
|
const normalizedCustomDomain = normalizeStatusPageDomain(customDomain);
|
||||||
const statusPage = new StatusPageModel({
|
const statusPage = new StatusPageModel({
|
||||||
...restData,
|
...restData,
|
||||||
customDomain: normalizeStatusPageDomain(customDomain),
|
...(normalizedCustomDomain ? { customDomain: normalizedCustomDomain } : {}),
|
||||||
userId,
|
userId,
|
||||||
teamId,
|
teamId,
|
||||||
});
|
});
|
||||||
@@ -133,20 +134,35 @@ class MongoStatusPagesRepository implements IStatusPagesRepository {
|
|||||||
): Promise<StatusPage> => {
|
): Promise<StatusPage> => {
|
||||||
const { logo, removeLogo, customDomain, ...restPatch } = patch;
|
const { logo, removeLogo, customDomain, ...restPatch } = patch;
|
||||||
void logo;
|
void logo;
|
||||||
const updateData: StatusPageUpdateData = { ...restPatch };
|
const setData: StatusPageUpdateData = { ...restPatch };
|
||||||
|
const unsetData: Record<string, 1> = {};
|
||||||
|
|
||||||
if (customDomain !== undefined) {
|
if (customDomain !== undefined) {
|
||||||
updateData.customDomain = normalizeStatusPageDomain(customDomain);
|
const normalizedCustomDomain = normalizeStatusPageDomain(customDomain);
|
||||||
|
if (normalizedCustomDomain) {
|
||||||
|
setData.customDomain = normalizedCustomDomain;
|
||||||
|
} else {
|
||||||
|
unsetData.customDomain = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (image) {
|
if (image) {
|
||||||
updateData.logo = {
|
setData.logo = {
|
||||||
data: image.buffer as Buffer,
|
data: image.buffer as Buffer,
|
||||||
contentType: image.mimetype,
|
contentType: image.mimetype,
|
||||||
};
|
};
|
||||||
} else if (removeLogo === "true") {
|
} 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,
|
new: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -67,11 +67,8 @@ const StatusPageSchema = new Schema<StatusPageDocument>(
|
|||||||
},
|
},
|
||||||
customDomain: {
|
customDomain: {
|
||||||
type: String,
|
type: String,
|
||||||
default: null,
|
|
||||||
lowercase: true,
|
lowercase: true,
|
||||||
trim: true,
|
trim: true,
|
||||||
sparse: true,
|
|
||||||
unique: true,
|
|
||||||
},
|
},
|
||||||
timezone: {
|
timezone: {
|
||||||
type: String,
|
type: String,
|
||||||
@@ -136,6 +133,14 @@ const StatusPageSchema = new Schema<StatusPageDocument>(
|
|||||||
{ timestamps: true }
|
{ timestamps: true }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
StatusPageSchema.index(
|
||||||
|
{ customDomain: 1 },
|
||||||
|
{
|
||||||
|
unique: true,
|
||||||
|
partialFilterExpression: { customDomain: { $type: "string" } },
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const StatusPageModel = model<StatusPageDocument>("StatusPage", StatusPageSchema);
|
const StatusPageModel = model<StatusPageDocument>("StatusPage", StatusPageSchema);
|
||||||
|
|
||||||
export type { StatusPageDocument };
|
export type { StatusPageDocument };
|
||||||
|
|||||||
@@ -134,6 +134,15 @@ describe("StatusPageService", () => {
|
|||||||
|
|
||||||
expect(repo.create).toHaveBeenCalledWith("user-1", "team-1", undefined, expect.objectContaining({ customDomain: "status.example.com" }));
|
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", () => {
|
describe("getStatusPagesByTeamId", () => {
|
||||||
|
|||||||
Reference in new issue
Block a user