fix: Document.findByPk() with and (#6208)
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import { EmptyResultError } from "sequelize";
|
||||||
import slugify from "@shared/utils/slugify";
|
import slugify from "@shared/utils/slugify";
|
||||||
import Document from "@server/models/Document";
|
import Document from "@server/models/Document";
|
||||||
import {
|
import {
|
||||||
@@ -176,6 +177,34 @@ describe("#findByPk", () => {
|
|||||||
const response = await Document.findByPk(id);
|
const response = await Document.findByPk(id);
|
||||||
expect(response?.id).toBe(document.id);
|
expect(response?.id).toBe(document.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should test with rejectOnEmpty flag", async () => {
|
||||||
|
const user = await buildUser();
|
||||||
|
const document = await buildDocument({
|
||||||
|
teamId: user.teamId,
|
||||||
|
createdById: user.id,
|
||||||
|
});
|
||||||
|
await expect(
|
||||||
|
Document.findByPk(document.id, {
|
||||||
|
userId: user.id,
|
||||||
|
rejectOnEmpty: true,
|
||||||
|
})
|
||||||
|
).resolves.not.toBeNull();
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
Document.findByPk(document.urlId, {
|
||||||
|
userId: user.id,
|
||||||
|
rejectOnEmpty: true,
|
||||||
|
})
|
||||||
|
).resolves.not.toBeNull();
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
Document.findByPk("0e8280ea-7b4c-40e5-98ba-ec8a2f00f5e8", {
|
||||||
|
userId: user.id,
|
||||||
|
rejectOnEmpty: true,
|
||||||
|
})
|
||||||
|
).rejects.toThrow(EmptyResultError);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("tasks", () => {
|
describe("tasks", () => {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import {
|
|||||||
FindOptions,
|
FindOptions,
|
||||||
ScopeOptions,
|
ScopeOptions,
|
||||||
WhereOptions,
|
WhereOptions,
|
||||||
|
EmptyResultError,
|
||||||
} from "sequelize";
|
} from "sequelize";
|
||||||
import {
|
import {
|
||||||
ForeignKey,
|
ForeignKey,
|
||||||
@@ -58,6 +59,7 @@ export const DOCUMENT_VERSION = 2;
|
|||||||
type AdditionalFindOptions = {
|
type AdditionalFindOptions = {
|
||||||
userId?: string;
|
userId?: string;
|
||||||
includeState?: boolean;
|
includeState?: boolean;
|
||||||
|
rejectOnEmpty?: boolean | Error;
|
||||||
};
|
};
|
||||||
|
|
||||||
@DefaultScope(() => ({
|
@DefaultScope(() => ({
|
||||||
@@ -513,22 +515,36 @@ class Document extends ParanoidModel {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
if (isUUID(id)) {
|
if (isUUID(id)) {
|
||||||
return scope.findOne({
|
const document = await scope.findOne({
|
||||||
where: {
|
where: {
|
||||||
id,
|
id,
|
||||||
},
|
},
|
||||||
...rest,
|
...rest,
|
||||||
|
rejectOnEmpty: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!document && rest.rejectOnEmpty) {
|
||||||
|
throw new EmptyResultError(`Document doesn't exist with id: ${id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return document;
|
||||||
}
|
}
|
||||||
|
|
||||||
const match = id.match(SLUG_URL_REGEX);
|
const match = id.match(SLUG_URL_REGEX);
|
||||||
if (match) {
|
if (match) {
|
||||||
return scope.findOne({
|
const document = await scope.findOne({
|
||||||
where: {
|
where: {
|
||||||
urlId: match[1],
|
urlId: match[1],
|
||||||
},
|
},
|
||||||
...rest,
|
...rest,
|
||||||
|
rejectOnEmpty: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!document && rest.rejectOnEmpty) {
|
||||||
|
throw new EmptyResultError(`Document doesn't exist with id: ${id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return document;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user