Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion models/requests.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import firestore from "../utils/firestore";
const requestModel = firestore.collection("requests");
import { REQUEST_ALREADY_APPROVED, REQUEST_ALREADY_REJECTED, REQUEST_STATE } from "../constants/requests";
import { REQUEST_ALREADY_APPROVED, REQUEST_ALREADY_REJECTED, REQUEST_STATE, REQUEST_TYPE } from "../constants/requests";
import {
ERROR_WHILE_FETCHING_REQUEST,
ERROR_WHILE_CREATING_REQUEST,
ERROR_WHILE_UPDATING_REQUEST,
REQUEST_DOES_NOT_EXIST,
} from "../constants/requests";
import { getUserId } from "../utils/users";
import { transformGetOooRequest } from "../utils/requests";
const SIZE = 5;

export const createRequest = async (body: any) => {
Expand Down Expand Up @@ -149,6 +150,10 @@ export const getRequests = async (query: any) => {
return null;
}

if (type === REQUEST_TYPE.OOO) {
allRequests = await transformGetOooRequest(dev, allRequests);
}

return {
allRequests,
prev: prevDoc.empty ? null : prevDoc.docs[0].id,
Expand Down
15 changes: 15 additions & 0 deletions types/oooRequest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ export type OooStatusRequest = {
updatedAt: Timestamp;
comment: string | null;
};

export type OldOooRequest = {
id: string;
type: REQUEST_TYPE.OOO;
from: number;
until: number;
message: string;
state: REQUEST_STATE;
lastModifiedBy: string | null;
requestedBy: string;
reason: string | null;
createdAt: Timestamp;
updatedAt: Timestamp;
};

export type OooStatusRequestBody = {
from: number;
until: number;
Expand Down
65 changes: 65 additions & 0 deletions utils/requests.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { ERROR_WHILE_FETCHING_REQUEST } from "../constants/requests";
import { fetchUser } from "../models/users";
import { userData } from "../types/global";
import { OldOooRequest, OooStatusRequest } from "../types/oooRequest";

/**
* Calculates the new deadline based on the current date, the old end date, and the additional duration in milliseconds.
*
Expand Down Expand Up @@ -31,4 +36,64 @@ export const convertDateStringToMilliseconds = (date: string): { isDate: boolean
isDate: true,
milliseconds,
};
};

export const transformGetOooRequest = async (dev, allRequests) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is temporary then what is the need of putting this function inside utils?
Can't we put the entire thing on that if block in model and put a comment that this will eventually get removed once everything is fixed?

cc: @AnujChhikara

const oooRequests = [];

if (dev) {
for (const request of allRequests) {
if (request.status) {
const modifiedRequest: OldOooRequest = {
id: request.id,
type: request.type,
from: request.from,
until: request.until,
message: request.reason,
state: request.status,
lastModifiedBy: request.lastModifiedBy ?? "",
requestedBy: request.userId,
reason: request.comment ?? "",
createdAt: request.createdAt,
updatedAt: request.updatedAt
};
oooRequests.push(modifiedRequest);
} else {
oooRequests.push(request);
}
}
} else {
for (const request of allRequests) {
if (request.state) {
try {
const userResponse: any = await fetchUser({ userId: request.requestedBy });
const username = userResponse.user.username;

const modifiedRequest: OooStatusRequest = {
id: request.id,
type: request.type,
from: request.from,
until: request.until,
reason: request.message,
status: request.state,
lastModifiedBy: request.lastModifiedBy ?? null,
requestedBy: username,
comment: request.reason ?? null,
createdAt: request.createdAt,
updatedAt: request.updatedAt,
userId: request.requestedBy
};

oooRequests.push(modifiedRequest);
} catch (error) {
logger.error(ERROR_WHILE_FETCHING_REQUEST, error);
throw error;
}
} else {
oooRequests.push(request);
}
}
}

return oooRequests;
};
Loading