Skip to content

Commit

Permalink
chore: rename to createLogicBody
Browse files Browse the repository at this point in the history
  • Loading branch information
tshuli committed May 17, 2021
1 parent c820222 commit 34d09ca
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/app/models/form.server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,11 +685,11 @@ const compileFormModel = (db: Mongoose): IFormModel => {
FormSchema.statics.createFormLogic = async function (
this: IFormModel,
formId: string,
createdLogic: LogicDto,
createLogicBody: LogicDto,
): Promise<IFormSchema | null> {
return this.findByIdAndUpdate(
formId,
{ $push: { form_logics: createdLogic } },
{ $push: { form_logics: createLogicBody } },
{
new: true,
runValidators: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7842,7 +7842,7 @@ describe('admin-form.controller', () => {
],
}

const mockCreatedLogic = {
const mockCreateLogicBody = {
_id: logicId,
} as ILogicSchema

Expand All @@ -7863,7 +7863,7 @@ describe('admin-form.controller', () => {
_id: MOCK_USER_ID,
},
},
body: mockCreatedLogic,
body: mockCreateLogicBody,
})
const mockRes = expressHandler.mockResponse()

Expand All @@ -7873,7 +7873,7 @@ describe('admin-form.controller', () => {
okAsync(MOCK_FORM),
)
MockAdminFormService.createFormLogic.mockReturnValue(
okAsync(mockCreatedLogic),
okAsync(mockCreateLogicBody),
)
})

Expand All @@ -7892,11 +7892,11 @@ describe('admin-form.controller', () => {
)
expect(MockAdminFormService.createFormLogic).toHaveBeenCalledWith(
MOCK_FORM,
mockCreatedLogic,
mockCreateLogicBody,
)

expect(mockRes.status).toHaveBeenCalledWith(200)
expect(mockRes.json).toHaveBeenCalledWith(mockCreatedLogic)
expect(mockRes.json).toHaveBeenCalledWith(mockCreateLogicBody)
})

it('should return 403 when user does not have permissions to update logic', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,7 @@ describe('admin-form.service', () => {
],
}

const createdLogic = {
const createLogicBody = {
logicType: LogicType.PreventSubmit,
} as ILogicSchema

Expand Down Expand Up @@ -1685,45 +1685,60 @@ describe('admin-form.service', () => {
CREATE_SPY.mockResolvedValue(mockEmailFormUpdated as IFormSchema)

// Act
const actualResult = await createFormLogic(mockEmailForm, createdLogic)
const actualResult = await createFormLogic(mockEmailForm, createLogicBody)

// Assert
expect(actualResult.isOk()).toEqual(true)
expect(actualResult._unsafeUnwrap()).toEqual(
expect.objectContaining(createdLogic),
expect.objectContaining(createLogicBody),
)

expect(CREATE_SPY).toHaveBeenCalledWith(mockEmailForm._id, createdLogic)
expect(CREATE_SPY).toHaveBeenCalledWith(
mockEmailForm._id,
createLogicBody,
)
})

it('should return ok(created logic) on successful form logic create for encrypt mode form', async () => {
// Arrange
CREATE_SPY.mockResolvedValue(mockEncryptFormUpdated as IFormSchema)

// Act
const actualResult = await createFormLogic(mockEncryptForm, createdLogic)
const actualResult = await createFormLogic(
mockEncryptForm,
createLogicBody,
)

// Assert
expect(actualResult.isOk()).toEqual(true)
expect(actualResult._unsafeUnwrap()).toEqual(
expect.objectContaining(createdLogic),
expect.objectContaining(createLogicBody),
)

expect(CREATE_SPY).toHaveBeenCalledWith(mockEncryptFormId, createdLogic)
expect(CREATE_SPY).toHaveBeenCalledWith(
mockEncryptFormId,
createLogicBody,
)
})

it('should return err(FormNotFoundError) if db does not return form object', async () => {
// Arrange
CREATE_SPY.mockResolvedValue((undefined as unknown) as IFormSchema)

// Act
const actualResult = await createFormLogic(mockEncryptForm, createdLogic)
const actualResult = await createFormLogic(
mockEncryptForm,
createLogicBody,
)

// Assert
expect(actualResult.isErr()).toEqual(true)
expect(actualResult._unsafeUnwrapErr()).toEqual(new FormNotFoundError())

expect(CREATE_SPY).toHaveBeenCalledWith(mockEncryptFormId, createdLogic)
expect(CREATE_SPY).toHaveBeenCalledWith(
mockEncryptFormId,
createLogicBody,
)
})

it('should return err(DatabaseError) if db returns form object that does not have form_logics array', async () => {
Expand All @@ -1735,13 +1750,19 @@ describe('admin-form.service', () => {
CREATE_SPY.mockResolvedValue(updatedFormWithoutLogic as IFormSchema)

// Act
const actualResult = await createFormLogic(mockEncryptForm, createdLogic)
const actualResult = await createFormLogic(
mockEncryptForm,
createLogicBody,
)

// Assert
expect(actualResult.isErr()).toEqual(true)
expect(actualResult._unsafeUnwrapErr()).toEqual(new DatabaseError())

expect(CREATE_SPY).toHaveBeenCalledWith(mockEncryptFormId, createdLogic)
expect(CREATE_SPY).toHaveBeenCalledWith(
mockEncryptFormId,
createLogicBody,
)
})

it('should return err(DatabaseError) if db returns form object that has empty form_logics array', async () => {
Expand All @@ -1753,13 +1774,19 @@ describe('admin-form.service', () => {
CREATE_SPY.mockResolvedValue(updatedFormWithEmptyLogic as IFormSchema)

// Act
const actualResult = await createFormLogic(mockEncryptForm, createdLogic)
const actualResult = await createFormLogic(
mockEncryptForm,
createLogicBody,
)

// Assert
expect(actualResult.isErr()).toEqual(true)
expect(actualResult._unsafeUnwrapErr()).toEqual(new DatabaseError())

expect(CREATE_SPY).toHaveBeenCalledWith(mockEncryptFormId, createdLogic)
expect(CREATE_SPY).toHaveBeenCalledWith(
mockEncryptFormId,
createLogicBody,
)
})
})

Expand Down
6 changes: 3 additions & 3 deletions src/app/modules/form/admin-form/admin-form.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1660,7 +1660,7 @@ export const _handleCreateLogic: RequestHandler<
LogicDto
> = (req, res) => {
const { formId } = req.params
const createdLogic = req.body
const createLogicBody = req.body
const sessionUserId = (req.session as Express.AuthedSession).user._id

// Step 1: Retrieve currently logged in user.
Expand All @@ -1676,7 +1676,7 @@ export const _handleCreateLogic: RequestHandler<
)
// Step 3: Create form logic
.andThen((retrievedForm) =>
AdminFormService.createFormLogic(retrievedForm, createdLogic),
AdminFormService.createFormLogic(retrievedForm, createLogicBody),
)
.map((createdLogic) => res.status(StatusCodes.OK).json(createdLogic))
.mapErr((error) => {
Expand All @@ -1687,7 +1687,7 @@ export const _handleCreateLogic: RequestHandler<
...createReqMeta(req),
userId: sessionUserId,
formId,
createdLogic,
createLogicBody,
},
error,
})
Expand Down
8 changes: 4 additions & 4 deletions src/app/modules/form/admin-form/admin-form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,24 +720,24 @@ export const updateFormSettings = (
/**
* Creates form logic.
* @param form The original form to create logic in
* @param createdLogic Object containing the created logic
* @param createLogicBody Object containing the created logic
* @returns ok(created logic dto) on success
* @returns err(database errors) if db error is thrown during logic update
*/
export const createFormLogic = (
form: IPopulatedForm,
createdLogic: LogicDto,
createLogicBody: LogicDto,
): ResultAsync<ILogicSchema, DatabaseError | FormNotFoundError> => {
// Create new form logic
return ResultAsync.fromPromise(
FormModel.createFormLogic(form._id, createdLogic),
FormModel.createFormLogic(form._id, createLogicBody),
(error) => {
logger.error({
message: 'Error occurred when creating form logic',
meta: {
action: 'createFormLogic',
formId: form._id,
createdLogic,
createLogicBody,
},
error,
})
Expand Down
4 changes: 2 additions & 2 deletions src/public/services/AdminFormService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ export const updateFormEndPage = async (

export const createFormLogic = async (
formId: string,
createdLogic: LogicDto,
createLogicBody: LogicDto,
): Promise<LogicDto> => {
return axios
.post<LogicDto>(`${ADMIN_FORM_ENDPOINT}/${formId}/logic`, createdLogic)
.post<LogicDto>(`${ADMIN_FORM_ENDPOINT}/${formId}/logic`, createLogicBody)
.then(({ data }) => data)
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export interface IFormModel extends Model<IFormSchema> {
): Promise<IPopulatedForm | null>
createFormLogic(
formId: string,
createdLogic: LogicDto,
createLogicBody: LogicDto,
): Promise<IFormSchema | null>
deleteFormLogic(formId: string, logicId: string): Promise<IFormSchema | null>
/**
Expand Down

0 comments on commit 34d09ca

Please sign in to comment.