From 2c181d49b06d79233f8f4f7064d5f842856cdbc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Mur=C3=A7a?= Date: Tue, 29 Nov 2022 11:36:55 -0300 Subject: [PATCH] Use switch case to create instance --- src/services/user-service-provider.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/services/user-service-provider.js b/src/services/user-service-provider.js index 98224fc..31f51cf 100644 --- a/src/services/user-service-provider.js +++ b/src/services/user-service-provider.js @@ -2,18 +2,21 @@ export const UserServiceProvider = (function () { let instance; async function createInstance(user) { - if (user.role === 'STUDENT') { - const service = await import('../services/student-service'); - if (service) { - return new service.default(user); - } - } else if (user.role === 'PROFESSOR') { - const service = await import('../services/professor-service'); - if (service) { - return new service.default(user); - } - } else { - throw new Error('Invalid Role!'); + switch (user.role) { + case 'STUDENT': + const studentService = await import('../services/student-service'); + if (studentService) { + return new studentService.default(user); + } + break; + case 'PROFESSOR': + const professorService = await import('../services/professor-service'); + if (professorService) { + return new professorService.default(user); + } + break; + default: + throw new Error('Invalid Role!'); } }