Passed
Push — trunk ( 17b9ed...85c15e )
by Christian
12:23 queued 13s
created

src/Administration/Resources/app/administration/src/app/init-post/user-information.init.ts   A

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 37
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 3
mnd 2
bc 2
fnc 1
bpm 2
cpm 3
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A user-information.init.ts ➔ initializeUserContext 0 28 3
1
/**
2
 * @package admin
3
 */
4
5
import { initializeUserNotifications } from 'src/app/state/notification.store';
6
7
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
8
export default function initializeUserContext() {
9
    return new Promise<void>((resolve) => {
10
        const loginService = Shopware.Service('loginService');
11
        const userService = Shopware.Service('userService');
12
13
        // The user isn't logged in
14
        if (!loginService.isLoggedIn()) {
15
            // Remove existing login info from the locale storage
16
            loginService.logout();
17
            resolve();
18
            return;
19
        }
20
21
        userService.getUser().then((response) => {
22
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
23
            const data = response?.data;
24
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
25
            delete data.password;
26
27
            Shopware.State.commit('setCurrentUser', data);
28
            initializeUserNotifications();
29
            resolve();
30
        }).catch(() => {
31
            // An error occurred which means the user isn't logged in so get rid of the information in local storage
32
            loginService.logout();
33
            resolve();
34
        });
35
    });
36
}
37