<commit_before>module.exports = (service) => {

	// Getting member system agent
	const memberAgent = service.getContext().get('Member')[service.memberAgent];
	const Permission = memberAgent.getPermissionMiddleware();

	// Create a new router
	const router = service.createRouter({
		prefix: '/api/v1'
	});

	// Set type to "API" for router
	router.use(service.routeType('API'));

	/**
	 * @api {post} /api/v1/members/forgot Request to reset password
	 * @apiName Forgot
	 * @apiGroup Member
	 *
	 * @apiParam {String} email member's email
	 *
	 * @apiError 403 Account is disabled
	 * @apiError 404 Account doesn't exist
	 * @apiError 422 Parameters are invalid
	 * @apiErrorExample {json} Error-Response:
	 *	HTTP/1.1 422 Validation Failed
	 *	{
	 *		code: 'ValidationFailed',
	 *		message: 'Validation Failed',
	 *		errors: [
	 *			{ field: 'email', code: 'required'  },
	 *		]
	 *	}
	 **/
	router.post('/members/forgot', async (ctx, next) => {

		const payload = ctx.request.body;

		try {
			// Verify member account
			const member = await memberAgent
				.getMemberManager()
				.sendPasswordResetEmail(payload);

			// Response
			ctx.body = {
				code: 'Success'
			};

		} catch(e) {

			switch(e.name) {
			case 'ValidationFailed':
				ctx.throw(422, {
					code: 'ValidationFailed',
					message: 'Validation failed',
					errors: e.errors.map((error) => {
						switch(error.type) {
						case 'any.required':
							return {
								field: error.field,
								code: 'required'
							};

						default:
							return {
								field: error.field,
								code: 'invalid'
							};
						}
					})
				});

			case 'Disabled':
				ctx.throw(403, {
					code: 'AccountBlocked',
					message: 'Account was blocked'
				});

			case 'NotExist':
				ctx.throw(404, {
					code: 'NotExist',
					message: 'Account doesn\'t exist'
				});
			}

			throw e;
		}
	});

	/**
	 * @api {post} /api/v1/members/reset_password Reset password
	 * @apiName ResetPassword
	 * @apiGroup Member
	 *
	 * @apiHeader {String} authorization user's token
	 * @apiParam {String} password <PASSWORD>
	 *
	 * @apiError 403 Account is disabled
	 * @apiError 404 Account doesn't exist
	 * @apiError 422 Parameters are invalid
	 * @apiErrorExample {json} Error-Response:
	 *	HTTP/1.1 422 Validation Failed
	 *	{
	 *		code: 'ValidationFailed',
	 *		message: 'Validation Failed',
	 *		errors: [
	 *			{ field: 'password', code: 'required'  },
	 *		]
	 *	}
	 **/
	router.post('/members/reset_password', async (ctx, next) => {

		const payload = ctx.request.body;

		try {
			// Verify member account<commit_msg>Fixed a bug that internal server error if reset password token is incorrect<commit_after>module.exports = (service) => {

	// Getting member system agent
	const memberAgent = service.getContext().get('Member')[service.memberAgent];
	const Permission = memberAgent.getPermissionMiddleware();

	// Create a new router
	const router = service.createRouter({
		prefix: '/api/v1'
	});

	// Set type to "API" for router
	router.use(service.routeType('API'));

	/**
	 * @api {post} /api/v1/members/forgot Request to reset password
	 * @apiName Forgot
	 * @apiGroup Member
	 *
	 * @apiParam {String} email member's email
	 *
	 * @apiError 403 Account is disabled
	 * @apiError 404 Account doesn't exist
	 * @apiError 422 Parameters are invalid
	 * @apiErrorExample {json} Error-Response:
	 *	HTTP/1.1 422 Validation Failed
	 *	{
	 *		code: 'ValidationFailed',
	 *		message: 'Validation Failed',
	 *		errors: [
	 *			{ field: 'email', code: 'required'  },
	 *		]
	 *	}
	 **/
	router.post('/members/forgot', async (ctx, next) => {

		const payload = ctx.request.body;

		try {
			// Verify member account
			const member = await memberAgent
				.getMemberManager()
				.sendPasswordResetEmail(payload);

			// Response
			ctx.body = {
				code: 'Success'
			};

		} catch(e) {

			switch(e.name) {
			case 'ValidationFailed':
				ctx.throw(422, {
					code: 'ValidationFailed',
					message: 'Validation failed',
					errors: e.errors.map((error) => {
						switch(error.type) {
						case 'any.required':
							return {
								field: error.field,
								code: 'required'
							};

						default:
							return {
								field: error.field,
								code: 'invalid'
							};
						}
					})
				});

			case 'Disabled':
				ctx.throw(403, {
					code: 'AccountBlocked',
					message: 'Account was blocked'
				});

			case 'NotExist':
				ctx.throw(404, {
					code: 'NotExist',
					message: 'Account doesn\'t exist'
				});
			}

			throw e;
		}
	});

	/**
	 * @api {post} /api/v1/members/reset_password Reset password
	 * @apiName ResetPassword
	 * @apiGroup Member
	 *
	 * @apiHeader {String} authorization user's token
	 * @apiParam {String} password <PASSWORD>
	 *
	 * @apiError 403 Account is disabled
	 * @apiError 404 Account doesn't exist
	 * @apiError 422 Parameters are invalid
	 * @apiErrorExample {json} Error-Response:
	 *	HTTP/1.1 422 Validation Failed
	 *	{
	 *		code: 'ValidationFailed',
	 *		message: 'Validation Failed',
	 *		errors: [
	 *			{ field: 'password', code: 'required'  },
	 *		]
	 *	}
	 **/
	router.post('/members/reset_password', async (ctx, next) => {

		if (ctx.state.session === undefined) {
			ctx.throw(403);
		}

		const payload = ctx.request.body;

		try {

			// Verify member account
{'fraction_non_alphanumeric': 0.14180790960451978, 'fraction_numerical': 0.015630885122410548, 'mean_word_length': 2.7244039270687237, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6222516', 'n_tokens_mistral': 2179, 'n_tokens_neox': 1658, 'n_words': 423}