generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate validation and update logic in service file
- Loading branch information
Showing
2 changed files
with
133 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { logType } from "../constants/logs"; | ||
import { | ||
INVALID_REQUEST_DEADLINE, | ||
INVALID_REQUEST_TYPE, | ||
LOG_ACTION, | ||
PENDING_REQUEST_UPDATED, | ||
REQUEST_DOES_NOT_EXIST, | ||
REQUEST_LOG_TYPE, | ||
REQUEST_STATE, | ||
REQUEST_TYPE, | ||
UNAUTHORIZED_TO_UPDATE_REQUEST | ||
} from "../constants/requests"; | ||
import { OnboardingExtension, UpdateOnboardingExtensionRequestBody } from "../types/onboardingExtension"; | ||
import { addLog } from "./logService"; | ||
import firestore from "../utils/firestore"; | ||
const requestModel = firestore.collection("requests"); | ||
|
||
export const validateOnboardingExtensionUpdateRequest = async ( | ||
extensionRequestDoc, | ||
id: string, | ||
isSuperuser: boolean, | ||
lastModifiedBy: string, | ||
newEndsOn: number | ||
) => { | ||
try{ | ||
|
||
if(!extensionRequestDoc.exists){ | ||
await addLog(logType.REQUEST_DOES_NOT_EXIST, { id }, { message: REQUEST_DOES_NOT_EXIST }); | ||
return { | ||
error: REQUEST_DOES_NOT_EXIST, | ||
} | ||
} | ||
|
||
const extensionRequest = extensionRequestDoc.data() as OnboardingExtension; | ||
|
||
if(!isSuperuser && lastModifiedBy !== extensionRequest.userId) { | ||
await addLog(logType.UNAUTHORIZED_TO_UPDATE_REQUEST, | ||
{ lastModifiedBy, userId: extensionRequest.userId }, | ||
{ message: UNAUTHORIZED_TO_UPDATE_REQUEST } | ||
); | ||
return { | ||
error: UNAUTHORIZED_TO_UPDATE_REQUEST | ||
}; | ||
} | ||
|
||
if(extensionRequest.type !== REQUEST_TYPE.ONBOARDING) { | ||
await addLog(logType.INVALID_REQUEST_TYPE, | ||
{ type: extensionRequest.type }, | ||
{ message: INVALID_REQUEST_TYPE } | ||
); | ||
|
||
return { | ||
error: INVALID_REQUEST_TYPE | ||
}; | ||
} | ||
|
||
if(extensionRequest.state != REQUEST_STATE.PENDING){ | ||
await addLog(logType.PENDING_REQUEST_CAN_BE_UPDATED, | ||
{ state: extensionRequest.state }, | ||
{ message:PENDING_REQUEST_UPDATED } | ||
); | ||
return { | ||
error: PENDING_REQUEST_UPDATED | ||
}; | ||
} | ||
|
||
if(extensionRequest.oldEndsOn >= newEndsOn) { | ||
await addLog(logType.INVALID_REQUEST_DEADLINE, | ||
{ oldEndsOn: extensionRequest.oldEndsOn, newEndsOn: newEndsOn }, | ||
{ message: INVALID_REQUEST_DEADLINE } | ||
); | ||
return { | ||
error: INVALID_REQUEST_DEADLINE | ||
}; | ||
} | ||
}catch(error){ | ||
logger.error("Error while validating onboarding extension update request", error); | ||
throw error; | ||
} | ||
} | ||
|
||
export const updateOnboardingExtensionRequest = async ( | ||
extensionRequestDoc, | ||
body: UpdateOnboardingExtensionRequestBody, | ||
lastModifiedBy: string) => { | ||
try{ | ||
const requestBody = { | ||
...body, | ||
lastModifiedBy, | ||
updatedAt: Date.now(), | ||
} | ||
|
||
await requestModel.doc(extensionRequestDoc.id).update(requestBody); | ||
|
||
const requestLog = { | ||
type: REQUEST_LOG_TYPE.REQUEST_UPDATED, | ||
meta: { | ||
requestId: extensionRequestDoc.id, | ||
action: LOG_ACTION.UPDATE, | ||
createdBy: lastModifiedBy, | ||
}, | ||
body: requestBody, | ||
}; | ||
|
||
await addLog(requestLog.type, requestLog.meta, requestLog.body); | ||
|
||
return requestBody; | ||
}catch(error){ | ||
logger.error("Error while updating onboarding extension request", error); | ||
throw error; | ||
} | ||
} |