mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-15 16:52:38 +00:00
Minor improvements on new reminders logic
This commit is contained in:
@@ -19,11 +19,21 @@ export const reminderDALFactory = (db: TDbClient) => {
|
||||
const reminderOrm = ormify(db, TableName.Reminder);
|
||||
|
||||
const getTodayDateRange = () => {
|
||||
const now = new Date();
|
||||
const startOfDay = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 0, 0, 0, 0));
|
||||
const endOfDay = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 23, 59, 59, 999));
|
||||
const today = new Date();
|
||||
const year = today.getUTCFullYear();
|
||||
const month = today.getUTCMonth();
|
||||
const date = today.getUTCDate();
|
||||
|
||||
return { startOfDay, endOfDay };
|
||||
// Start of day: 00:00:00.000 UTC
|
||||
const startOfDay = new Date(Date.UTC(year, month, date, 0, 0, 0, 0));
|
||||
|
||||
// End of day: 23:59:59.999 UTC
|
||||
const endOfDay = new Date(Date.UTC(year, month, date, 23, 59, 59, 999));
|
||||
|
||||
return {
|
||||
startOfDay,
|
||||
endOfDay
|
||||
};
|
||||
};
|
||||
|
||||
const findSecretDailyReminders = async (tx?: Knex) => {
|
||||
|
||||
@@ -6,7 +6,7 @@ import { QueueJobs, QueueName, TQueueServiceFactory } from "@app/queue";
|
||||
|
||||
import { TSecretReminderRecipientsDALFactory } from "../secret-reminder-recipients/secret-reminder-recipients-dal";
|
||||
import { TSecretV2BridgeDALFactory } from "../secret-v2-bridge/secret-v2-bridge-dal";
|
||||
import { TReminderServiceFactory } from "./reminder-service";
|
||||
import { TReminderServiceFactory } from "./reminder-types";
|
||||
|
||||
type TDailyReminderQueueServiceFactoryDep = {
|
||||
reminderService: TReminderServiceFactory;
|
||||
|
||||
@@ -13,7 +13,7 @@ import { TReminderRecipientDALFactory } from "../reminder-recipients/reminder-re
|
||||
import { TSecretV2BridgeDALFactory } from "../secret-v2-bridge/secret-v2-bridge-dal";
|
||||
import { SmtpTemplates, TSmtpService } from "../smtp/smtp-service";
|
||||
import { TReminderDALFactory } from "./reminder-dal";
|
||||
import { TBatchCreateReminderDTO, TCreateReminderDTO } from "./reminder-types";
|
||||
import { TBatchCreateReminderDTO, TCreateReminderDTO, TReminderServiceFactory } from "./reminder-types";
|
||||
|
||||
type TReminderServiceFactoryDep = {
|
||||
reminderDAL: TReminderDALFactory;
|
||||
@@ -24,8 +24,6 @@ type TReminderServiceFactoryDep = {
|
||||
secretV2BridgeDAL: Pick<TSecretV2BridgeDALFactory, "invalidateSecretCacheByProjectId">;
|
||||
};
|
||||
|
||||
export type TReminderServiceFactory = ReturnType<typeof reminderServiceFactory>;
|
||||
|
||||
export const reminderServiceFactory = ({
|
||||
reminderDAL,
|
||||
reminderRecipientDAL,
|
||||
@@ -33,8 +31,8 @@ export const reminderServiceFactory = ({
|
||||
projectMembershipDAL,
|
||||
permissionService,
|
||||
secretV2BridgeDAL
|
||||
}: TReminderServiceFactoryDep) => {
|
||||
const addDays = (days: number, fromDate: Date = new Date()): Date => {
|
||||
}: TReminderServiceFactoryDep): TReminderServiceFactory => {
|
||||
const $addDays = (days: number, fromDate: Date = new Date()): Date => {
|
||||
const result = new Date(fromDate);
|
||||
result.setDate(result.getDate() + days);
|
||||
return result;
|
||||
@@ -74,7 +72,7 @@ export const reminderServiceFactory = ({
|
||||
}
|
||||
};
|
||||
|
||||
const createReminderInternal = async ({
|
||||
const createReminderInternal: TReminderServiceFactory["createReminderInternal"] = async ({
|
||||
secretId,
|
||||
message,
|
||||
repeatDays,
|
||||
@@ -98,7 +96,7 @@ export const reminderServiceFactory = ({
|
||||
}
|
||||
|
||||
if (repeatDays && repeatDays > 0) {
|
||||
nextReminderDate = addDays(repeatDays);
|
||||
nextReminderDate = $addDays(repeatDays);
|
||||
}
|
||||
|
||||
if (!nextReminderDate) {
|
||||
@@ -133,7 +131,7 @@ export const reminderServiceFactory = ({
|
||||
return { id: reminderId, created: !existingReminder };
|
||||
};
|
||||
|
||||
const createReminder = async ({
|
||||
const createReminder: TReminderServiceFactory["createReminder"] = async ({
|
||||
actor,
|
||||
actorId,
|
||||
actorOrgId,
|
||||
@@ -157,7 +155,7 @@ export const reminderServiceFactory = ({
|
||||
return response;
|
||||
};
|
||||
|
||||
const getReminder = async ({
|
||||
const getReminder: TReminderServiceFactory["getReminder"] = async ({
|
||||
secretId,
|
||||
projectId,
|
||||
actor,
|
||||
@@ -187,7 +185,7 @@ export const reminderServiceFactory = ({
|
||||
return reminder;
|
||||
};
|
||||
|
||||
const sendDailyReminders = async () => {
|
||||
const sendDailyReminders: TReminderServiceFactory["sendDailyReminders"] = async () => {
|
||||
const remindersToSend = await reminderDAL.findSecretDailyReminders();
|
||||
|
||||
for (const reminder of remindersToSend) {
|
||||
@@ -211,7 +209,7 @@ export const reminderServiceFactory = ({
|
||||
}
|
||||
});
|
||||
if (reminder.repeatDays) {
|
||||
await reminderDAL.updateById(reminder.id, { nextReminderDate: addDays(reminder.repeatDays) }, tx);
|
||||
await reminderDAL.updateById(reminder.id, { nextReminderDate: $addDays(reminder.repeatDays) }, tx);
|
||||
} else {
|
||||
await reminderDAL.deleteById(reminder.id, tx);
|
||||
}
|
||||
@@ -225,7 +223,7 @@ export const reminderServiceFactory = ({
|
||||
}
|
||||
};
|
||||
|
||||
const deleteReminder = async ({
|
||||
const deleteReminder: TReminderServiceFactory["deleteReminder"] = async ({
|
||||
actor,
|
||||
actorId,
|
||||
actorOrgId,
|
||||
@@ -253,7 +251,11 @@ export const reminderServiceFactory = ({
|
||||
await secretV2BridgeDAL.invalidateSecretCacheByProjectId(projectId);
|
||||
};
|
||||
|
||||
const removeReminderRecipients = async (secretId: string, projectId: string, tx?: Knex) => {
|
||||
const removeReminderRecipients: TReminderServiceFactory["removeReminderRecipients"] = async (
|
||||
secretId: string,
|
||||
projectId: string,
|
||||
tx?: Knex
|
||||
) => {
|
||||
const reminder = await reminderDAL.findOne({ secretId }, tx);
|
||||
if (!reminder) {
|
||||
return;
|
||||
@@ -262,12 +264,19 @@ export const reminderServiceFactory = ({
|
||||
await secretV2BridgeDAL.invalidateSecretCacheByProjectId(projectId);
|
||||
};
|
||||
|
||||
const deleteReminderBySecretId = async (secretId: string, projectId: string, tx?: Knex) => {
|
||||
const deleteReminderBySecretId: TReminderServiceFactory["deleteReminderBySecretId"] = async (
|
||||
secretId: string,
|
||||
projectId: string,
|
||||
tx?: Knex
|
||||
) => {
|
||||
await reminderDAL.delete({ secretId }, tx);
|
||||
await secretV2BridgeDAL.invalidateSecretCacheByProjectId(projectId);
|
||||
};
|
||||
|
||||
const batchCreateReminders = async (remindersData: TBatchCreateReminderDTO, tx?: Knex) => {
|
||||
const batchCreateReminders: TReminderServiceFactory["batchCreateReminders"] = async (
|
||||
remindersData: TBatchCreateReminderDTO,
|
||||
tx?: Knex
|
||||
) => {
|
||||
if (!remindersData || remindersData.length === 0) {
|
||||
return { created: 0, reminderIds: [] };
|
||||
}
|
||||
@@ -280,7 +289,7 @@ export const reminderServiceFactory = ({
|
||||
}
|
||||
|
||||
if (repeatDays && repeatDays > 0 && !nextReminderDate) {
|
||||
nextReminderDate = addDays(repeatDays);
|
||||
nextReminderDate = $addDays(repeatDays);
|
||||
}
|
||||
|
||||
if (!nextReminderDate) {
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import { Knex } from "knex";
|
||||
|
||||
import { ActorAuthMethod, ActorType } from "../auth/auth-type";
|
||||
|
||||
export type TReminder = {
|
||||
id: string;
|
||||
secretId?: string;
|
||||
message?: string;
|
||||
repeatDays?: number;
|
||||
nextReminderDate?: Date;
|
||||
secretId?: string | null;
|
||||
message?: string | null;
|
||||
repeatDays?: number | null;
|
||||
nextReminderDate: Date;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
};
|
||||
|
||||
export type TCreateReminderDTO = {
|
||||
@@ -32,3 +35,82 @@ export type TBatchCreateReminderDTO = {
|
||||
recipients?: string[] | null;
|
||||
projectId?: string;
|
||||
}[];
|
||||
|
||||
export interface TReminderServiceFactory {
|
||||
createReminder: ({
|
||||
actor,
|
||||
actorId,
|
||||
actorOrgId,
|
||||
actorAuthMethod,
|
||||
projectId,
|
||||
reminder
|
||||
}: TCreateReminderDTO) => Promise<{
|
||||
id: string;
|
||||
created: boolean;
|
||||
}>;
|
||||
|
||||
getReminder: ({
|
||||
secretId,
|
||||
projectId,
|
||||
actor,
|
||||
actorId,
|
||||
actorOrgId,
|
||||
actorAuthMethod
|
||||
}: {
|
||||
secretId: string;
|
||||
projectId: string;
|
||||
actor: ActorType;
|
||||
actorId: string;
|
||||
actorOrgId: string;
|
||||
actorAuthMethod: ActorAuthMethod;
|
||||
}) => Promise<(TReminder & { recipients: string[] }) | null>;
|
||||
|
||||
sendDailyReminders: () => Promise<void>;
|
||||
|
||||
deleteReminder: ({
|
||||
actor,
|
||||
actorId,
|
||||
actorOrgId,
|
||||
actorAuthMethod,
|
||||
secretId,
|
||||
projectId
|
||||
}: {
|
||||
actor: ActorType;
|
||||
actorId: string;
|
||||
actorOrgId: string;
|
||||
actorAuthMethod: ActorAuthMethod;
|
||||
secretId: string;
|
||||
projectId: string;
|
||||
}) => Promise<void>;
|
||||
|
||||
removeReminderRecipients: (secretId: string, projectId: string, tx?: Knex) => Promise<void>;
|
||||
|
||||
deleteReminderBySecretId: (secretId: string, projectId: string, tx?: Knex) => Promise<void>;
|
||||
|
||||
batchCreateReminders: (
|
||||
remindersData: TBatchCreateReminderDTO,
|
||||
tx?: Knex
|
||||
) => Promise<{
|
||||
created: number;
|
||||
reminderIds: string[];
|
||||
}>;
|
||||
|
||||
createReminderInternal: ({
|
||||
secretId,
|
||||
message,
|
||||
repeatDays,
|
||||
nextReminderDate,
|
||||
recipients,
|
||||
projectId
|
||||
}: {
|
||||
secretId?: string;
|
||||
message?: string | null;
|
||||
repeatDays?: number | null;
|
||||
nextReminderDate?: string | null;
|
||||
recipients?: string[] | null;
|
||||
projectId: string;
|
||||
}) => Promise<{
|
||||
id: string;
|
||||
created: boolean;
|
||||
}>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user