fix: fix lint errors

This commit is contained in:
lemmyMwaura
2024-08-07 23:26:24 +03:00
parent 9061ec2dff
commit 069651bdb4
4 changed files with 48 additions and 39 deletions
@@ -115,7 +115,7 @@ export const registerSecretSharingRouter = async (server: FastifyZodProvider) =>
tag: true, tag: true,
expiresAt: true, expiresAt: true,
expiresAfterViews: true, expiresAfterViews: true,
accessType: true, accessType: true
}).extend({ }).extend({
orgName: z.string().optional() orgName: z.string().optional()
}) })
@@ -1,12 +1,19 @@
import bcrypt from "bcrypt"; import bcrypt from "bcrypt";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service"; import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service";
import { BadRequestError, ForbiddenRequestError, InternalServerError, NotFoundError, UnauthorizedError } from "@app/lib/errors"; import {
BadRequestError,
ForbiddenRequestError,
InternalServerError,
NotFoundError,
UnauthorizedError
} from "@app/lib/errors";
import { SecretSharingAccessType } from "@app/lib/types"; import { SecretSharingAccessType } from "@app/lib/types";
import { TOrgDALFactory } from "../org/org-dal"; import { TOrgDALFactory } from "../org/org-dal";
import { TSecretSharingDALFactory } from "./secret-sharing-dal"; import { TSecretSharingDALFactory } from "./secret-sharing-dal";
import { import {
SharedSecretWithDate,
TCreatePublicSharedSecretDTO, TCreatePublicSharedSecretDTO,
TCreateSharedSecretDTO, TCreateSharedSecretDTO,
TDeleteSharedSecretDTO, TDeleteSharedSecretDTO,
@@ -162,6 +169,40 @@ export const secretSharingServiceFactory = ({
}; };
}; };
/** Checks if secret is expired and throws error if true */
const checkIfExpired = async (sharedSecret: SharedSecretWithDate, sharedSecretId: string) => {
const { expiresAt, expiresAfterViews } = sharedSecret;
if (expiresAt !== null && expiresAt < new Date()) {
// check lifetime expiry
await secretSharingDAL.softDeleteById(sharedSecretId);
throw new ForbiddenRequestError({
message: "Access denied: Secret has expired by lifetime"
});
}
if (expiresAfterViews !== null && expiresAfterViews === 0) {
// check view count expiry
await secretSharingDAL.softDeleteById(sharedSecretId);
throw new ForbiddenRequestError({
message: "Access denied: Secret has expired by view count"
});
}
};
const decrementSecretViewCount = async (sharedSecret: SharedSecretWithDate, sharedSecretId: string) => {
const { expiresAfterViews } = sharedSecret;
if (expiresAfterViews) {
// decrement view count if view count expiry set
await secretSharingDAL.updateById(sharedSecretId, { $decr: { expiresAfterViews: 1 } });
}
await secretSharingDAL.updateById(sharedSecretId, {
lastViewedAt: new Date()
});
};
const getActiveSharedSecretById = async ({ sharedSecretId, hashedHex, orgId }: TGetActiveSharedSecretByIdDTO) => { const getActiveSharedSecretById = async ({ sharedSecretId, hashedHex, orgId }: TGetActiveSharedSecretByIdDTO) => {
const sharedSecret = await secretSharingDAL.findOne({ const sharedSecret = await secretSharingDAL.findOne({
id: sharedSecretId, id: sharedSecretId,
@@ -223,7 +264,7 @@ export const secretSharingServiceFactory = ({
}); });
const isMatch = await bcrypt.compare(password, sharedSecret.password as string); const isMatch = await bcrypt.compare(password, sharedSecret.password as string);
if (!isMatch) return undefined if (!isMatch) return undefined;
// we reduce the view count when we are sure the password matches. // we reduce the view count when we are sure the password matches.
await decrementSecretViewCount(sharedSecret, sharedSecretId); await decrementSecretViewCount(sharedSecret, sharedSecretId);
@@ -245,40 +286,6 @@ export const secretSharingServiceFactory = ({
return deletedSharedSecret; return deletedSharedSecret;
}; };
/** Checks if secret is expired and throws error if true */
const checkIfExpired = async (sharedSecret: any, sharedSecretId: string) => {
const { expiresAt, expiresAfterViews } = sharedSecret;
if (expiresAt !== null && expiresAt < new Date()) {
// check lifetime expiry
await secretSharingDAL.softDeleteById(sharedSecretId);
throw new ForbiddenRequestError({
message: "Access denied: Secret has expired by lifetime"
});
}
if (expiresAfterViews !== null && expiresAfterViews === 0) {
// check view count expiry
await secretSharingDAL.softDeleteById(sharedSecretId);
throw new ForbiddenRequestError({
message: "Access denied: Secret has expired by view count"
});
}
}
const decrementSecretViewCount = async (sharedSecret: any, sharedSecretId: string) => {
const { expiresAfterViews } = sharedSecret;
if (expiresAfterViews) {
// decrement view count if view count expiry set
await secretSharingDAL.updateById(sharedSecretId, { $decr: { expiresAfterViews: 1 } });
}
await secretSharingDAL.updateById(sharedSecretId, {
lastViewedAt: new Date()
});
}
return { return {
createSharedSecret, createSharedSecret,
createPublicSharedSecret, createPublicSharedSecret,
@@ -41,6 +41,8 @@ export type TValidateActiveSharedSecretDTO = TGetActiveSharedSecretByIdDTO & {
export type TCreateSharedSecretDTO = TSharedSecretPermission & TCreatePublicSharedSecretDTO; export type TCreateSharedSecretDTO = TSharedSecretPermission & TCreatePublicSharedSecretDTO;
export type SharedSecretWithDate = Omit<TCreateSharedSecretDTO, 'expiresAt'> & { expiresAt: Date };
export type TDeleteSharedSecretDTO = { export type TDeleteSharedSecretDTO = {
sharedSecretId: string; sharedSecretId: string;
} & TSharedSecretPermission; } & TSharedSecretPermission;
@@ -42,11 +42,11 @@ export const PasswordContainer = ({ secretId, hashedHex, handleSecret }: Props)
if (secret) { if (secret) {
handleSecret(secret); handleSecret(secret);
} else { } else {
reset({ password: "" });
createNotification({ createNotification({
text: "Password is Invalid. Try again", text: "Password is Invalid. Try again",
type: "error" type: "error"
}) })
reset();
} }
} catch (error) { } catch (error) {
console.error("Failed to validate password:", error); console.error("Failed to validate password:", error);