fix: simplify deleted query
This commit is contained in:
@@ -26,115 +26,6 @@ export type FindDocumentsOptions = {
|
|||||||
senderIds?: number[];
|
senderIds?: number[];
|
||||||
};
|
};
|
||||||
|
|
||||||
const getDeletedFilter = (
|
|
||||||
status: ExtendedDocumentStatus,
|
|
||||||
user: User,
|
|
||||||
team?: (Team & { teamEmail: TeamEmail | null }) | null,
|
|
||||||
) => {
|
|
||||||
if (status === ExtendedDocumentStatus.BIN) {
|
|
||||||
return {
|
|
||||||
OR: [
|
|
||||||
{
|
|
||||||
userId: user.id,
|
|
||||||
deletedAt: {
|
|
||||||
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Recipient: {
|
|
||||||
some: {
|
|
||||||
email: user.email,
|
|
||||||
documentDeletedAt: {
|
|
||||||
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
...(team
|
|
||||||
? [
|
|
||||||
{
|
|
||||||
teamId: team.id,
|
|
||||||
deletedAt: {
|
|
||||||
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
...(team.teamEmail
|
|
||||||
? [
|
|
||||||
{
|
|
||||||
User: {
|
|
||||||
email: team.teamEmail.email,
|
|
||||||
},
|
|
||||||
deletedAt: {
|
|
||||||
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Recipient: {
|
|
||||||
some: {
|
|
||||||
email: team.teamEmail.email,
|
|
||||||
documentDeletedAt: {
|
|
||||||
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
]
|
|
||||||
: []),
|
|
||||||
]
|
|
||||||
: []),
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
AND: {
|
|
||||||
OR: [
|
|
||||||
{
|
|
||||||
userId: user.id,
|
|
||||||
deletedAt: null,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Recipient: {
|
|
||||||
some: {
|
|
||||||
email: user.email,
|
|
||||||
documentDeletedAt: null,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
...(team
|
|
||||||
? team.teamEmail
|
|
||||||
? [
|
|
||||||
{
|
|
||||||
teamId: team.id,
|
|
||||||
deletedAt: null,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
User: {
|
|
||||||
email: team.teamEmail.email,
|
|
||||||
},
|
|
||||||
deletedAt: null,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Recipient: {
|
|
||||||
some: {
|
|
||||||
email: team.teamEmail.email,
|
|
||||||
documentDeletedAt: null,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
]
|
|
||||||
: [
|
|
||||||
{
|
|
||||||
teamId: team.id,
|
|
||||||
deletedAt: null,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
: []),
|
|
||||||
],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export const findDocuments = async ({
|
export const findDocuments = async ({
|
||||||
userId,
|
userId,
|
||||||
teamId,
|
teamId,
|
||||||
@@ -190,12 +81,9 @@ export const findDocuments = async ({
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const deletedFilter = getDeletedFilter(status, user, team);
|
|
||||||
|
|
||||||
const whereClause: Prisma.DocumentWhereInput = {
|
const whereClause: Prisma.DocumentWhereInput = {
|
||||||
...termFilters,
|
...termFilters,
|
||||||
...filters,
|
...filters,
|
||||||
...deletedFilter,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (period) {
|
if (period) {
|
||||||
@@ -356,7 +244,6 @@ const findDocumentsFilter = (status: ExtendedDocumentStatus, user: User) => {
|
|||||||
{
|
{
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
teamId: null,
|
teamId: null,
|
||||||
status: ExtendedDocumentStatus.COMPLETED,
|
|
||||||
deletedAt: {
|
deletedAt: {
|
||||||
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
|
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
|
||||||
},
|
},
|
||||||
@@ -554,19 +441,17 @@ const findTeamDocumentsFilter = (
|
|||||||
return filter;
|
return filter;
|
||||||
})
|
})
|
||||||
.with(ExtendedDocumentStatus.BIN, () => {
|
.with(ExtendedDocumentStatus.BIN, () => {
|
||||||
const filter: Prisma.DocumentWhereInput = {
|
const filters: Prisma.DocumentWhereInput[] = [
|
||||||
OR: [
|
|
||||||
{
|
{
|
||||||
teamId: team.id,
|
teamId: team.id,
|
||||||
deletedAt: {
|
deletedAt: {
|
||||||
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
|
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
];
|
||||||
};
|
|
||||||
|
|
||||||
if (teamEmail && filter.OR) {
|
if (teamEmail) {
|
||||||
filter.OR.push(
|
filters.push(
|
||||||
{
|
{
|
||||||
User: {
|
User: {
|
||||||
email: teamEmail,
|
email: teamEmail,
|
||||||
@@ -588,7 +473,9 @@ const findTeamDocumentsFilter = (
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return filter;
|
return {
|
||||||
|
OR: filters,
|
||||||
|
};
|
||||||
})
|
})
|
||||||
|
|
||||||
.exhaustive();
|
.exhaustive();
|
||||||
|
|||||||
@@ -197,9 +197,7 @@ type GetTeamCountsOption = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const getTeamCounts = async (options: GetTeamCountsOption) => {
|
const getTeamCounts = async (options: GetTeamCountsOption) => {
|
||||||
const { createdAt, teamId, teamEmail } = options;
|
const { createdAt, teamId, teamEmail, senderIds = [] } = options;
|
||||||
|
|
||||||
const senderIds = options.senderIds ?? [];
|
|
||||||
|
|
||||||
const userIdWhereClause: Prisma.DocumentWhereInput['userId'] =
|
const userIdWhereClause: Prisma.DocumentWhereInput['userId'] =
|
||||||
senderIds.length > 0
|
senderIds.length > 0
|
||||||
@@ -298,9 +296,13 @@ const getTeamCounts = async (options: GetTeamCountsOption) => {
|
|||||||
_all: true,
|
_all: true,
|
||||||
},
|
},
|
||||||
where: {
|
where: {
|
||||||
userId: userIdWhereClause,
|
|
||||||
createdAt,
|
|
||||||
OR: [
|
OR: [
|
||||||
|
{
|
||||||
|
teamId,
|
||||||
|
deletedAt: {
|
||||||
|
gte: DateTime.now().minus({ days: 30 }).startOf('day').toJSDate(),
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
status: ExtendedDocumentStatus.PENDING,
|
status: ExtendedDocumentStatus.PENDING,
|
||||||
Recipient: {
|
Recipient: {
|
||||||
|
|||||||
@@ -205,8 +205,6 @@ export const documentRouter = router({
|
|||||||
requestMetadata: extractNextApiRequestMetadata(ctx.req),
|
requestMetadata: extractNextApiRequestMetadata(ctx.req),
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(restoredDocument);
|
|
||||||
|
|
||||||
return restoredDocument;
|
return restoredDocument;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|||||||
Reference in New Issue
Block a user