Passed
Push — trunk ( 024f41...cfd10b )
by Christian
15:08 queued 13s
created

user-settings.mixin.ts ➔ saveUserSettings   B

Complexity

Conditions 7

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
dl 0
loc 41
rs 7.8319
c 0
b 0
f 0
1
/**
2
 * @package admin
3
 */
4
5
/* @private */
6
export {};
7
8
/**
9
 * @deprecated tag:v6.6.0 - Will be private
10
 */
11
Shopware.Mixin.register('user-settings', {
12
    inject: [
13
        'acl',
14
    ],
15
16
    computed: {
17
        userConfigRepository() {
18
            return this.repositoryFactory.create('user_config');
19
        },
20
21
        currentUser() {
22
            return Shopware.State.get('session').currentUser;
23
        },
24
    },
25
26
    methods: {
27
        /**
28
         * Receives the whole settings entity via identifier key
29
         *
30
         * @param {string} identifier Used to identify its target use
31
         * @param {string|null} userId Id of the target user; `null` will use the current user
32
         * @return {Promise<*>}
33
         */
34
        getUserSettingsEntity(identifier: string, userId: string|null = null) {
35
            if (!this.acl.can('user_config:read')) {
36
                return Promise.reject();
37
            }
38
39
            return this.userConfigRepository.search(
40
                this.userGridSettingsCriteria(identifier, userId),
41
                Shopware.Context.api,
42
            ).then((response) => {
43
                if (!response.length) {
44
                    return null;
45
                }
46
47
                return response[0];
48
            });
49
        },
50
51
        /**
52
         * Receives settings values via identifier key
53
         *
54
         * @param {string} identifier Used to identify its target use
55
         * @param {string|null} userId Id of the target user; `null` will use the current user
56
         * @return {Promise<*>}
57
         */
58
        async getUserSettings(identifier: string, userId = null) {
59
            const entity = await this.getUserSettingsEntity(identifier, userId);
60
61
            if (!entity) {
62
                return null;
63
            }
64
65
            return entity.value;
66
        },
67
68
        /**
69
         * Saves settings to the currently logged in user
70
         *
71
         * @param {string} identifier Unique key to identify its target use
72
         * @param {{[key: string]: any}} entityValue Values to save
73
         * @param {string|null} userId Id of the target user; `null` will use the current user
74
         * @return {Promise<*>}
75
         */
76
        async saveUserSettings(identifier: string, entityValue: {
77
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
78
            [key: string]: any;
79
        }, userId: string|null = null) {
80
            if (!this.acl.can('user_config:create') || !this.acl.can('user_config:update')) {
81
                return Promise.reject();
82
            }
83
84
            if (!identifier) {
85
                return Promise.reject();
86
            }
87
88
            if (!identifier.includes('.')) {
89
                identifier = `custom.${identifier}`;
90
            }
91
92
            if (!userId) {
93
                userId = this.currentUser?.id;
94
            }
95
96
            let userSettings = await this.getUserSettingsEntity(identifier);
97
            if (!userSettings) {
98
                userSettings = this.userConfigRepository.create(Shopware.Context.api);
99
            }
100
101
            const entityData = Object.assign(userSettings, {
102
                userId,
103
                key: identifier,
104
                value: entityValue,
105
            });
106
107
            return this.userConfigRepository.save(entityData, Shopware.Context.api);
108
        },
109
110
        /**
111
         * Provides the userSettings criteria used for the queries
112
         *
113
         * @internal
114
         * @param {string} identifier Used to identify its target use
115
         * @param {string|null} userId Id of the target user; `null` will use the current user
116
         * @return {Criteria}
117
         */
118
        userGridSettingsCriteria(identifier: string, userId: string|null = null) {
119
            if (!userId) {
120
                userId = this.currentUser?.id;
121
            }
122
123
            const criteria = new Shopware.Data.Criteria(1, 25);
124
            criteria.addFilter(Shopware.Data.Criteria.equals('key', identifier));
125
            criteria.addFilter(Shopware.Data.Criteria.equals('userId', userId));
126
127
            return criteria;
128
        },
129
    },
130
});
131