182 lines
6.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { addedUser, getUserById, updatedUser, usersWithThisService, usersWithoutThisService, addServiceIdToUser, deleteServiceIdFromUser } from "./helpers"
class UsersOfServices {
constructor(adapter, store) {
this.adapter = adapter
this.store = store
}
/**
*
* @param {String} mode
* @returns {Promise<void>}
*/
async getUsers(mode) {
const users = await this.adapter.getUsers({mode})
this.store.dispatch('users/updateUsers', users)
return users
}
/**
*
* @param {Number} serviceId
* @param {String} mode
* @returns {Promise<void>}
*/
async getUsersByService(serviceId, mode) {
const users = await this.adapter.getUsers({mode})
const usersWithService = await this.adapter.getUsersByServiceId(serviceId, mode)
const usersWithoutService = usersWithoutThisService(serviceId, users)
this.store.dispatch('users/updateUsers', users)
this.store.dispatch('users/updateUsersWithService', usersWithService)
this.store.dispatch('users/updateUsersWithoutService', usersWithoutService)
return usersWithService
}
/**
*
* @param {Number} serviceId
* @returns {Promise<void>}
*/
async getUsersWithoutService(serviceId) {
const users = this.store.getters['users/users']
const filteredUsers = usersWithoutThisService(serviceId, users)
this.store.dispatch('users/updateUsersWithoutService', filteredUsers)
return filteredUsers
}
/**
*
* @returns {Promise<Object>}
*/
async addNewUser() {
const newUser = {id: -1, firstName: '',lastName: '', role: '', email: '', password: '', serviceId: []}
this.store.dispatch('users/updateSelectedUser', newUser)
return newUser
}
/**
*
* @returns {Promise<Object>}
*/
async getSelectedUser() {
const selectedUser = this.store.getters['users/selectedUser']
return selectedUser
}
/**
*
* @param {Number} selectedUserId
* @returns {Promise<Object>}
*/
async setSelectedUser(selectedUserId) {
const selectedUser = getUserById(selectedUserId, this.store.getters['users/users'])
this.store.dispatch('users/updateSelectedUser', selectedUser)
return selectedUser
}
/**
*
* @param {String} status
* @returns {Promise<String>}
*/
async setStatusUser(status) {
this.store.dispatch('users/updateUserStatus', status)
return status
}
/**
*
* @param {Number} selectedUserId
* @param {Number} serviceId
* @returns {Promise<Object>}
*/
async addUserToService(selectedUserId, serviceId) {
// const addedUser = await this.adapter.addUser(selectedUserId, serviceId) // Запрос на обновление пользователя с новым service_id
const users = this.store.getters['users/users']
const updatedUsers = addServiceIdToUser(selectedUserId, serviceId, users)
const usersWithService = usersWithThisService(serviceId, updatedUsers)
const usersWithoutService = usersWithoutThisService(serviceId, updatedUsers)
this.store.dispatch('users/updateUsers', updatedUsers)
this.store.dispatch('users/updateUsersWithService', usersWithService)
this.store.dispatch('users/updateUsersWithoutService', usersWithoutService)
}
/**
*
* @param {String} key - field name
* @param {Any} value - updated data for selected user, send to server
* @returns {Promise<Object>}
*/
async setUserFields(key, value) {
const selectedUser = this.store.getters['users/selectedUser']
if (selectedUser) {
selectedUser[key] = value
this.store.dispatch('users/updateSelectedUser', selectedUser)
return selectedUser
}
}
/**
*
* @param {Number} serviceId - updated data for selected user, send to server
* @returns {Promise<Object>}
*/
async createUser(serviceId) {
const selectedUser = this.store.getters['users/selectedUser']
if (selectedUser && selectedUser.id && selectedUser.id === -1) {
const users = this.store.getters['users/users']
const userData = {...selectedUser,
id: users.length + 1, // временно пока нет данных с сервера
serviceId: [serviceId],
}
// const createdUser = await this.adapter.createUser(userData) // Запрос на создание нового пользователя
// const updatedUsers = addedUser(createdUser, users)
const updatedUsers = addedUser(userData, users)
const usersWithService = usersWithThisService(serviceId, updatedUsers)
const usersWithoutService = usersWithoutThisService(serviceId, updatedUsers)
this.store.dispatch('users/updateUsers', updatedUsers)
this.store.dispatch('users/updateUsersWithService', usersWithService)
this.store.dispatch('users/updateUsersWithoutService', usersWithoutService)
}
}
/**
*
* @returns {Promise<Object>}
*/
async updateUser() {
const selectedUser = this.store.getters['users/selectedUser']
if (selectedUser && selectedUser.id) {
// const updatedUser = await this.adapter.updateUser(selectedUser) // Запрос на обновление пользователя с новыми данными
const users = this.store.getters['users/usersWithService']
const updatedUsers = updatedUser(selectedUser, users)
this.store.dispatch('users/updateUsersWithService', updatedUsers)
}
}
/**
*
* @param {Number} selectedUserId
* @param {Number} serviceId
* @returns {Promise<Object>}
*/
async deleteUserFromService(selectedUserId, serviceId) {
// const deletedUser = await this.adapter.addUser(selectedUserId, serviceId) // Запрос на обновление пользователя с новым service_id???
const users = this.store.getters['users/users']
const updatedUsers = deleteServiceIdFromUser(selectedUserId, serviceId, users)
const usersWithService = usersWithThisService(serviceId, updatedUsers)
const usersWithoutService = usersWithoutThisService(serviceId, updatedUsers)
this.store.dispatch('users/updateUsers', updatedUsers)
this.store.dispatch('users/updateUsersWithService', usersWithService)
this.store.dispatch('users/updateUsersWithoutService', usersWithoutService)
}
async resetStore() {
this.store.dispatch('users/resetStore')
}
}
export default UsersOfServices