Skip to content

Commit

Permalink
[doc: typedefs utilities] (PalisadoesFoundation#1183)
Browse files Browse the repository at this point in the history
  • Loading branch information
evasharma12 authored Mar 16, 2023
1 parent b22b9cb commit 728d884
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/typeDefs/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { gql } from "apollo-server-core";

/**
* This graphQL typeDef defines the logic for different mutations defined in the talawa-api.
*/
// Place fields alphabetically to ensure easier lookup and navigation.
export const mutations = gql`
type Mutation {
Expand Down
4 changes: 3 additions & 1 deletion src/typeDefs/queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { gql } from "apollo-server-core";

/**
* This graphQL typeDef defines the logic for different queries defined in the talawa-api.
*/
// Place fields alphabetically to ensure easier lookup and navigation.
export const queries = gql`
type Query {
Expand Down
9 changes: 8 additions & 1 deletion src/utilities/adminCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { Types } from "mongoose";
import { errors, requestContext } from "../libraries";
import { USER_NOT_AUTHORIZED_ADMIN } from "../constants";
import { Interface_Organization, User } from "../models";

/**
* If the current user is an admin of the organisation, this function returns `true` otherwise it returns `false`.
* @remarks
* This is a utility method.
* @param userId - Current user id.
* @param organization - Organization data of `Interface_Organization` type.
* @returns `True` or `False`.
*/
export const adminCheck = async (
userId: string | Types.ObjectId,
organization: Interface_Organization
Expand Down
7 changes: 6 additions & 1 deletion src/utilities/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ export interface Interface_JwtTokenPayload {
lastName: string;
email: string;
}

/**
* This function creates a json web token which expires in 15 minutes.
* It signs the given payload(user data) into a JSON Web Token string payload.
* @param user - User data
* @returns JSON Web Token string payload
*/
export const createAccessToken = async (user: Interface_User) => {
return jwt.sign(
{
Expand Down
7 changes: 6 additions & 1 deletion src/utilities/copyToClipboard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import ncp from "copy-paste";
import { IN_PRODUCTION } from "../constants";

/**
* This utility function copy the text into the clipboard.
* @remarks
* This is a utility method. This works only in development or test mode.
* @param text - The content that need to be copied.
*/
export const copyToClipboard = (text: string) => {
// Only copies in development or test mode
if (IN_PRODUCTION !== true) {
Expand Down
5 changes: 4 additions & 1 deletion src/utilities/deleteDuplicatedImage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { unlink, PathLike } from "fs";
import { logger } from "../libraries";

/**
* This function deletes a duplicated image using the function fs.unlink().
* @param imagePath - Path of the image
*/
export const deleteDuplicatedImage = (imagePath: PathLike) => {
unlink(imagePath, function (error) {
if (error) {
Expand Down
8 changes: 7 additions & 1 deletion src/utilities/deleteImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { unlink } from "fs";
import { logger } from "../libraries";
import { ImageHash } from "../models";
import { reuploadDuplicateCheck } from "./reuploadDuplicateCheck";

/**
* This function deletes an image if it is only used once.
* It is also ensured that the image hash isn't used by multiple users/organization before deleting it
* After deleting the image, the number of uses of the hashed image are decremented by one.
* @param imageToBeDeleted - Path of image
* @param imageBelongingToItem - Does image belong to an item
*/
export const deleteImage = async (
imageToBeDeleted: string,
imageBelongingToItem?: string
Expand Down
13 changes: 8 additions & 5 deletions src/utilities/imageAlreadyInDbCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { reuploadDuplicateCheck } from "./reuploadDuplicateCheck";
import { errors, requestContext } from "../libraries";
import { INVALID_FILE_TYPE } from "../constants";

/*
Check to see if image already exists in db using hash
if its there point to that image and remove the image just uploaded
if its not there allow the file to remain uploaded
*/
/**
* This function checks if an image already exists in the database using hash.
* If it does, then point to that image and remove the image just uploaded.
* Else, allow the file to get uploaded.
* @param oldImagePath - Path of image
* @param newImagePath - Does image belong to an item
* @returns file name.
*/
export const imageAlreadyInDbCheck = async (
oldImagePath: string | null,
newImagePath: string
Expand Down
7 changes: 6 additions & 1 deletion src/utilities/imageExtensionCheck.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { deleteImage } from "./deleteImage";
import { errors, requestContext } from "../libraries";
import { INVALID_FILE_TYPE } from "../constants";

/**
* This function checks the extension of the file.
* If the extension isn't of type 'png', or 'jpg', or 'jpeg',
* then the file is deleted and a validation error is thrown.
* @param filename - Name of file
*/
export const imageExtensionCheck = async (filename: string) => {
const fileExtension = filename.split(".").pop();

Expand Down
9 changes: 8 additions & 1 deletion src/utilities/mailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ export interface Interface_MailFields {
subject: string;
body: string;
}

/**
* This function sends emails to the specified user using the node mailer module.
* @remarks
* This is a utility method.
*
* @param Interface_MailFields - `Interface` type with emailTo(`string`), subject(`string`), and body(`string`) necessary attributes.
* @returns Promise along with resolve and reject methods.
*/
export const mailer = (mailFields: Interface_MailFields) => {
// Nodemailer configuration
const transporter = nodemailer.createTransport({
Expand Down
12 changes: 11 additions & 1 deletion src/utilities/reuploadDuplicateCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ const getImageHash = (oldSrc: Type_ImagePath) => {
});
});
};

/**
* This function determines whether a user or an organisation is
* attempting to re-upload the same profile photo or organisation image.
*
* @remarks
* This is a utility method.
*
* @param oldImagePath - Path of a current Org/User image of `type: Type_ImagePath`.
* @param newImagePath - Path of a new image of `type: Type_ImagePath`.
* @returns If the identical image is trying to reuploaded, `true`; otherwise, `false`.
*/
export const reuploadDuplicateCheck = async (
oldImagePath: Type_ImagePath | null,
newImagePath: Type_ImagePath
Expand Down
9 changes: 8 additions & 1 deletion src/utilities/uploadImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import { logger } from "../libraries";
import { imageAlreadyInDbCheck } from "./imageAlreadyInDbCheck";
import { deleteImage } from "./deleteImage";
import { imageExtensionCheck } from "./imageExtensionCheck";

/**
* This function uploads the new image and deletes the previously uploaded image if exists.
* @remarks
* This is a utility method.
* @param newImageFile - File of a new Image with `any` type.
* @param oldImagePath - File of a current Image. It can be `null`.
* @returns Path of an uploaded image.
*/
export const uploadImage = async (
newImageFile: any,
oldImagePath: string | null
Expand Down

0 comments on commit 728d884

Please sign in to comment.