Files
freeplanet/src/store/Modules/Projects.ts

594 lines
18 KiB
TypeScript
Raw Normal View History

2019-04-27 20:12:41 +02:00
import { IProject, IProjectsState, IDrag, IMenuList, IAction } from 'model'
2021-02-24 04:52:33 +01:00
import { Privacy, TipoVisu } from '@src/model'
2019-03-22 20:51:42 +01:00
import { storeBuilder } from './Store/Store'
import Api from '@api'
import { tools } from './tools'
2019-07-12 14:09:44 +02:00
import { toolsext } from '@src/store/Modules/toolsext'
import { lists } from './lists'
2019-03-22 20:51:42 +01:00
import * as ApiTables from './ApiTables'
import { GlobalStore, UserStore } from '@store'
import globalroutines from './../../globalroutines/index'
import objectId from '@src/js/objectId'
import { costanti } from '@src/store/Modules/costanti'
import { RouteNames } from '@src/router/route-names'
import * as Types from '@src/store/Api/ApiTypes'
import { serv_constants } from '@src/store/Modules/serv_constants'
import { static_data } from '@src/db/static_data'
2019-03-22 20:51:42 +01:00
2019-03-28 12:58:34 +01:00
const nametable = 'projects'
2019-03-22 20:51:42 +01:00
// import _ from 'lodash'
const stateglob: IProjectsState = {
2019-03-22 20:51:42 +01:00
showtype: costanti.ShowTypeTask.SHOW_LAST_N_COMPLETED,
2019-03-28 12:58:34 +01:00
projects: [],
2019-03-22 20:51:42 +01:00
insidePending: false,
2019-04-29 01:01:31 +02:00
visuLastCompleted: 10
2019-03-22 20:51:42 +01:00
}
const listFieldsToChange: string [] = ['descr', 'respUsername', 'viceRespUsername', 'longdescr', 'hoursplanned', 'hoursleft', 'hoursworked', 'id_parent', 'statusproj',
'category', 'expiring_at', 'priority', 'pos', 'groupId', 'enableExpiring', 'progressCalc', 'live_url', 'test_url',
'begin_development', 'begin_test', 'actualphase', 'totalphases', 'hoursweeky_plannedtowork', 'endwork_estimate',
2021-02-24 04:52:33 +01:00
'privacyread', 'privacywrite', 'tipovisu', 'id_main_project', 'typeproj', 'favourite', 'themecolor', 'themebgcolor']
2019-04-29 01:01:31 +02:00
const listFieldsUpdateCalculation: string [] = ['hoursplanned', 'hoursleft', 'hoursworked', 'progressCalc', 'endwork_estimate']
2019-03-22 20:51:42 +01:00
const b = storeBuilder.module<IProjectsState>('Projects', stateglob)
2019-03-22 20:51:42 +01:00
const stateGetter = b.state()
2021-02-21 02:05:39 +01:00
function getFirstInherited(proj, idparent, state) {
let myprojtocheck = null
while (proj.privacyread === Privacy.inherited) {
myprojtocheck = state.projects.find((rec) => rec._id === idparent)
if (!myprojtocheck)
return null
idparent = myprojtocheck.id_parent
proj = myprojtocheck
}
return myprojtocheck
}
2021-02-24 04:52:33 +01:00
function getFirstInheritedTipoVisu(proj, idparent, state) {
let tipovisuproj = null
while (!proj.tipovisu || proj.tipovisu <= 0) {
tipovisuproj = state.projects.find((rec) => rec._id === idparent)
if (!tipovisuproj)
return null
idparent = tipovisuproj.id_parent
proj = tipovisuproj
}
return tipovisuproj.tipovisu
}
2019-03-28 12:58:34 +01:00
function getarrByCategory(category: string) {
if (!stateglob.projects) {
2019-03-22 20:51:42 +01:00
return []
}
return stateglob.projects
2019-03-22 20:51:42 +01:00
}
function initcat() {
const rec = Getters.getters.getRecordEmpty()
rec.userId = UserStore.state.my._id
return rec
2019-03-22 20:51:42 +01:00
}
function updateDataCalculated(projout, projin) {
listFieldsUpdateCalculation.forEach((field) => {
projout[field] = projin[field]
})
}
function getproj(projects, idproj, tipoproj: string) {
2019-10-10 16:53:33 +02:00
let ris = null
if (tipoproj === RouteNames.myprojects)
ris = projects.filter((proj) => (proj.id_parent === idproj) && (proj.userId === UserStore.state.my._id))
else if (tipoproj === RouteNames.projectsshared)
ris = projects.filter((proj) => (proj.id_parent === idproj) && (proj.userId === UserStore.state.my._id) && (proj.privacyread !== Privacy.onlyme))
else if (tipoproj === RouteNames.projectsall)
2021-02-21 02:05:39 +01:00
ris = projects.filter((proj) => (proj.id_parent === idproj) && (proj.userId !== UserStore.state.my._id))
2019-10-10 16:53:33 +02:00
if (ris)
ris = ris.sort((a, b) => a.pos - b.pos)
2019-10-10 16:53:33 +02:00
// console.log('idproj', idproj, 'projects', projects, 'getproj', tipoproj, 'ris=', ris)
return ris
}
2019-03-22 20:51:42 +01:00
namespace Getters {
const getRecordEmpty = b.read((state: IProjectsState) => (): IProject => {
2019-04-05 16:16:29 +02:00
// const tomorrow = tools.getDateNow()
// tomorrow.setDate(tomorrow.getDate() + 1)
const obj: IProject = {
_id: objectId(),
descr: '',
longdescr: '',
typeproj: 0,
id_parent: '',
id_main_project: '',
priority: tools.Priority.PRIORITY_NORMAL,
statusproj: tools.Status.OPENED,
2019-04-05 16:16:29 +02:00
created_at: tools.getDateNow(),
modify_at: tools.getDateNow(),
completed_at: tools.getDateNull(),
category: '',
// expiring_at: tomorrow,
enableExpiring: false,
pos: 0,
modified: false,
live_url: '',
test_url: '',
totalphases: 1,
actualphase: 1,
hoursworked: 0,
hoursplanned: 0,
2019-04-29 01:01:31 +02:00
hoursleft: 0,
2019-04-05 16:16:29 +02:00
progressCalc: 0,
2021-02-21 02:05:39 +01:00
privacyread: 'inherited',
privacywrite: 'inherited',
2019-04-05 16:16:29 +02:00
begin_development: tools.getDateNull(),
begin_test: tools.getDateNull(),
hoursweeky_plannedtowork: 0,
endwork_estimate: tools.getDateNull(),
themecolor: '',
themebgcolor: '',
groupId: '',
respUsername: '',
2021-02-24 04:52:33 +01:00
viceRespUsername: '',
tipovisu: 0
}
return obj
}, 'getRecordEmpty')
const projs_dacompletare = b.read((state: IProjectsState) => (id_parent: string, tipoproj: string): IProject[] => {
2019-07-10 11:37:00 +02:00
// console.log('projs_dacompletare')
2019-03-28 12:58:34 +01:00
if (state.projects) {
2019-10-10 16:53:33 +02:00
// console.log('projs_dacompletare', state.projects)
return getproj(state.projects, id_parent, tipoproj)
2019-03-22 20:51:42 +01:00
} else {
return []
}
}, 'projs_dacompletare')
2019-03-22 20:51:42 +01:00
const listaprojects = b.read((state: IProjectsState) => (tipoproj: string): IMenuList[] => {
if (state.projects) {
2019-07-10 11:37:00 +02:00
console.log('listaprojects')
const listaproj = getproj(state.projects, process.env.PROJECT_ID_MAIN, tipoproj)
const myarr: IMenuList[] = []
for (const proj of listaproj) {
myarr.push({ nametranslate: '', description: proj.descr, idelem: proj._id })
}
2019-10-10 16:53:33 +02:00
console.log(' myarr', myarr, listaproj)
return myarr
} else {
return []
}
}, 'listaprojects')
2021-02-21 02:05:39 +01:00
const listagerarchia = b.read((state: IProjectsState) => (tipoproj: string, idparent: string): IMenuList[] => {
if (state.projects) {
// console.log('listagerarchia', idparent)
const myarrgerarchia: IMenuList[] = []
let myidparent = idparent
let precmyparent = '-1'
while (state.projects && myidparent) {
const proj = state.projects.find((rec) => rec._id === myidparent)
if (proj) {
myarrgerarchia.push({ nametranslate: '', description: proj.descr, idelem: proj._id })
} else {
break
}
if (myidparent === proj.id_parent || (!proj.id_parent && (precmyparent === myidparent)))
break
precmyparent = myidparent
myidparent = proj.id_parent
}
// console.log(' myarrgerarchia', myarrgerarchia)
return myarrgerarchia.reverse()
} else {
return []
}
}, 'listagerarchia')
const getDescrById = b.read((state: IProjectsState) => (id: string): string => {
if (id === process.env.PROJECT_ID_MAIN)
return 'Projects'
2019-03-28 12:58:34 +01:00
if (state.projects) {
const itemtrov = state.projects.find((item) => item._id === id)
if (!!itemtrov)
return itemtrov.descr
2019-03-22 20:51:42 +01:00
}
return ''
}, 'getDescrById')
const getRecordById = b.read((state: IProjectsState) => (id: string): IProject => {
// console.log('state.projects', state.projects)
// console.log('find', state.projects.find((item) => item._id === id))
if (state.projects) {
return state.projects.find((item) => item._id === id)
2019-03-22 20:51:42 +01:00
}
return null
}, 'getRecordById')
2019-03-22 20:51:42 +01:00
const getifCanISeeProj = b.read((state: IProjectsState) => (proj: IProject): boolean => {
2019-04-25 00:30:13 +02:00
if ((proj === undefined) || (proj === null))
return false
if (!!UserStore.state) {
if (UserStore.state.my._id === proj.userId) // If it's the owner
return true
2021-02-21 02:05:39 +01:00
let myprojtocheck = proj
if (proj.privacyread === Privacy.inherited) {
myprojtocheck = getFirstInherited(proj, proj.id_parent, state)
if (!myprojtocheck)
return true
}
console.log('privacyread', myprojtocheck.privacyread)
return (UserStore.state.my._id === myprojtocheck.userId) || (myprojtocheck.privacyread === Privacy.all) ||
(myprojtocheck.privacyread === Privacy.friends) && (UserStore.getters.IsMyFriend(myprojtocheck.userId))
|| ((myprojtocheck.privacyread === Privacy.mygroup) && (UserStore.getters.IsMyGroup(myprojtocheck.userId)))
} else {
return false
}
}, 'getifCanISeeProj')
2021-02-24 04:52:33 +01:00
const getTipoVisuProj = b.read((state: IProjectsState) => (proj: IProject): number => {
if ((proj === undefined) || (proj === null))
return TipoVisu.simplelist
if (!!UserStore.state) {
let tipovisuproj = proj
if (tipovisuproj.tipovisu === TipoVisu.inherited) {
tipovisuproj = getFirstInheritedTipoVisu(proj, proj.id_parent, state)
if (!tipovisuproj)
return TipoVisu.simplelist
}
return tipovisuproj.tipovisu
} else {
return TipoVisu.simplelist
}
}, 'getTipoVisuProj')
const CanIModifyPanelPrivacy = b.read((state: IProjectsState) => (proj: IProject): boolean => {
2019-04-25 00:30:13 +02:00
if ((proj === undefined) || (proj === null))
return false
if (!!UserStore) {
2021-02-21 02:05:39 +01:00
let myprojtocheck = proj
if (proj.privacywrite === Privacy.inherited) {
myprojtocheck = getFirstInherited(proj, proj.id_parent, state)
if (!myprojtocheck)
return true
}
if (!!UserStore.state)
2021-02-21 02:05:39 +01:00
return (UserStore.state.my._id === myprojtocheck.userId) || (myprojtocheck.privacywrite === Privacy.all) ||
(myprojtocheck.privacywrite === Privacy.friends) && (UserStore.getters.IsMyFriend(myprojtocheck.userId))
|| ((myprojtocheck.privacywrite === Privacy.mygroup) && (UserStore.getters.IsMyGroup(myprojtocheck.userId)))
else
return false
}
return false
}, 'CanIModifyPanelPrivacy')
2019-03-22 20:51:42 +01:00
export const getters = {
get getifCanISeeProj() {
return getifCanISeeProj()
},
get CanIModifyPanelPrivacy() {
return CanIModifyPanelPrivacy()
},
2021-02-24 04:52:33 +01:00
get getTipoVisuProj() {
return getTipoVisuProj()
},
get getRecordEmpty() {
return getRecordEmpty()
},
get projs_dacompletare() {
return projs_dacompletare()
2019-03-22 20:51:42 +01:00
},
get listaprojects() {
return listaprojects()
},
2021-02-21 02:05:39 +01:00
get listagerarchia() {
return listagerarchia()
},
get getDescrById() {
return getDescrById()
2019-03-22 20:51:42 +01:00
},
get getRecordById() {
return getRecordById()
2019-03-22 20:51:42 +01:00
}
}
}
namespace Mutations {
function createNewItem(state: IProjectsState, { objproj, atfirst, categorySel }) {
// console.log('createNewItem', objproj, 'cat=', categorySel, 'state.projects', state.projects)
2019-03-28 12:58:34 +01:00
if (state.projects === undefined) {
state.projects = []
state.projects.push(objproj)
console.log('push state.projects', state.projects)
2019-03-22 20:51:42 +01:00
return
}
if (atfirst) {
2019-03-28 12:58:34 +01:00
state.projects.unshift(objproj)
2021-02-21 02:05:39 +01:00
} else {
2019-03-28 12:58:34 +01:00
state.projects.push(objproj)
2019-03-22 20:51:42 +01:00
}
}
2019-03-22 20:51:42 +01:00
function updateProject(state: IProjectsState, { objproj }) {
if (!!objproj) {
2021-02-21 02:05:39 +01:00
// console.log('updateProject', objproj)
const index = tools.getIndexById(state.projects, objproj._id)
2021-02-21 02:05:39 +01:00
// console.log('index', index)
if (index >= 0) {
updateDataCalculated(state.projects[index], objproj)
2019-03-22 20:51:42 +01:00
// state.projects.splice(index, 1, objproj)
// tools.notifyarraychanged(state.projects)
}
}
2019-03-22 20:51:42 +01:00
}
function deletemyitem(state: IProjectsState, myitem: IProject) {
// Find record
2019-03-28 12:58:34 +01:00
const ind = tools.getIndexById(state.projects, myitem._id)
2019-03-22 20:51:42 +01:00
2019-03-28 12:58:34 +01:00
ApiTables.removeitemfromarray(state.projects, ind)
2019-03-22 20:51:42 +01:00
}
2021-02-21 02:05:39 +01:00
async function movemyitem(state: IProjectsState, { myitemorig, myitemdest }) {
2019-04-27 20:12:41 +02:00
const indorig = tools.getIndexById(state.projects, myitemorig._id)
state.projects.splice(indorig, 1)
state.projects.push(myitemdest)
await Actions.actions.modify({ myitem: myitemdest, field: 'id_parent' })
}
2019-03-22 20:51:42 +01:00
export const mutations = {
deletemyitem: b.commit(deletemyitem),
createNewItem: b.commit(createNewItem),
2019-04-27 20:12:41 +02:00
updateProject: b.commit(updateProject),
movemyitem: b.commit(movemyitem)
2019-03-22 20:51:42 +01:00
}
}
namespace Actions {
async function dbLoad(context, { checkPending, onlyiffirsttime }) {
2019-10-10 16:53:33 +02:00
if (!static_data.functionality.ENABLE_PROJECTS_LOADING)
return null
if (onlyiffirsttime) {
if (stateglob.projects.length > 0) {
// if already set, then exit.
return new Types.AxiosError(0, null, 0, '')
}
}
2019-03-22 20:51:42 +01:00
// if (UserStore.state.my._id === '') {
// return false // Login not made
// }
2019-03-22 20:51:42 +01:00
console.log('UserStore.state.my', UserStore.state.my)
console.log('dbLoad', nametable, checkPending, 'userid=', UserStore.state.my._id)
const ris = await Api.SendReq('/projects/' + UserStore.state.my._id, 'GET', null)
2019-03-22 20:51:42 +01:00
.then((res) => {
2019-03-28 12:58:34 +01:00
if (res.data.projects) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
stateglob.projects = res.data.projects
2019-03-22 20:51:42 +01:00
} else {
stateglob.projects = []
2019-03-22 20:51:42 +01:00
}
stateglob.showtype = parseInt(GlobalStore.getters.getConfigStringbyId({
2019-03-22 20:51:42 +01:00
id: costanti.CONFIG_ID_SHOW_TYPE_TODOS,
default: costanti.ShowTypeTask.SHOW_LAST_N_COMPLETED
}), 10)
if (process.env.DEBUG === '1') {
console.log('dbLoad', 'state.projects', stateglob.projects)
2019-03-22 20:51:42 +01:00
}
return res
})
.catch((error) => {
2019-03-28 12:58:34 +01:00
console.log('error dbLoad', error)
2019-03-22 20:51:42 +01:00
UserStore.mutations.setErrorCatch(error)
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, tools.ERR_GENERICO, error)
2019-03-22 20:51:42 +01:00
})
ApiTables.aftercalling(ris, checkPending, nametable)
2019-03-22 20:51:42 +01:00
}
2021-02-11 02:21:36 +01:00
async function calculateHoursProjects(context, { projId, actualphase }) {
let ris = null
ris = await Api.SendReq('/projects/calc/' + projId + '/' + actualphase, 'GET', null)
.then((res) => {
if (res.data.rec) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
return res.data.rec
}
return null
})
.catch((error) => {
console.log('error calculateHoursProjects', error)
})
return ris
}
async function deleteItem(context, { idobj }) {
2019-03-22 20:51:42 +01:00
console.log('deleteItem: KEY = ', idobj)
const myarr = getarrByCategory('')
2019-03-22 20:51:42 +01:00
const myobjtrov = tools.getElemById(myarr, idobj)
console.log('myobjtrov', myobjtrov.descr)
if (!!myobjtrov) {
/*
2019-03-22 20:51:42 +01:00
const myobjnext = tools.getElemPrevById(myarr, myobjtrov._id)
if (!!myobjnext) {
myobjnext.pos = myobjtrov.pos + 1
2019-03-22 20:51:42 +01:00
myobjnext.modified = true
await modify(context, { myitem: myobjnext, field: 'pos' })
2019-03-22 20:51:42 +01:00
}
*/
// ApiTables.table_DeleteRecord(nametable, myobjtrov, idobj)
ApiTables.table_HideRecord(nametable, myobjtrov, idobj)
2019-03-22 20:51:42 +01:00
}
}
2019-03-28 12:58:34 +01:00
async function dbInsert(context, { myobj, atfirst }) {
2019-03-22 20:51:42 +01:00
const objproj = initcat()
objproj.descr = myobj.descr
objproj.category = myobj.category
objproj.id_parent = myobj.id_parent
objproj.id_main_project = myobj.id_main_project
objproj.typeproj = myobj.typeproj
2019-04-26 12:07:38 +02:00
objproj.privacyread = myobj.privacyread
objproj.privacywrite = myobj.privacywrite
objproj.actualphase = myobj.actualphase
2021-02-24 04:52:33 +01:00
objproj.tipovisu = myobj.tipovisu
2019-03-22 20:51:42 +01:00
let elemtochange: IProject = null
2019-03-28 12:58:34 +01:00
const myarr = getarrByCategory(objproj.category)
2019-03-22 20:51:42 +01:00
if (atfirst) {
console.log('INSERT AT THE TOP')
elemtochange = tools.getFirstList(myarr)
objproj.pos = 10
2019-03-22 20:51:42 +01:00
} else {
console.log('INSERT AT THE BOTTOM')
// INSERT AT THE BOTTOM , so GET LAST ITEM
const lastelem = tools.getLastListNotCompleted(nametable, objproj.id_parent, this.tipoProj)
2019-03-22 20:51:42 +01:00
objproj.pos = (!!lastelem) ? lastelem.pos + 10 : 10
2019-03-22 20:51:42 +01:00
}
objproj.modified = false
Mutations.mutations.createNewItem({ objproj, atfirst, categorySel: objproj.category }) // 1) Create record in Memory
const id = await globalroutines(context, 'write', nametable, objproj) // 2) Insert into the IndexedDb
let field = ''
if (atfirst) { // update also the last elem
if (!!elemtochange) {
elemtochange.pos = objproj.pos
2019-03-22 20:51:42 +01:00
console.log('elemtochange', elemtochange)
field = 'pos'
2019-03-22 20:51:42 +01:00
// Modify the other record
await modify(context, { myitem: elemtochange, field })
}
}
// 3) send to the Server
await ApiTables.Sync_SaveItem(nametable, 'POST', objproj)
return id
2019-03-22 20:51:42 +01:00
}
async function modify(context, { myitem, field }) {
2019-04-05 23:59:52 +02:00
return await ApiTables.table_ModifyRecord(nametable, myitem, listFieldsToChange, field)
2019-03-22 20:51:42 +01:00
}
async function swapElems(context, itemdragend: IDrag) {
console.log('PROJECT swapElems', itemdragend, stateglob.projects)
2019-03-22 20:51:42 +01:00
const myarr = Getters.getters.projs_dacompletare(itemdragend.id_proj, itemdragend.tipoproj)
2019-03-22 20:51:42 +01:00
2019-04-05 23:59:52 +02:00
tools.swapGeneralElem(nametable, myarr, itemdragend, listFieldsToChange)
2019-03-22 20:51:42 +01:00
}
2019-04-27 20:12:41 +02:00
async function ActionCutPaste(context, action: IAction) {
if (action.type === lists.MenuAction.CUT) {
2019-04-29 01:01:31 +02:00
GlobalStore.state.lastaction = action
} else if (action.type === lists.MenuAction.PASTE) {
if (GlobalStore.state.lastaction.type === lists.MenuAction.CUT) {
2019-04-27 20:12:41 +02:00
// Change id_parent
2019-04-29 01:01:31 +02:00
const orig_obj = Getters.getters.getRecordById(GlobalStore.state.lastaction._id)
2019-04-27 20:12:41 +02:00
const dest = Getters.getters.getRecordById(action._id)
// console.log('dest', dest)
const dest_obj = tools.jsonCopy(orig_obj)
if (!!dest_obj) {
dest_obj.id_parent = dest._id
dest_obj.id_main_project = dest.id_main_project
dest_obj.modified = true
2019-04-29 01:01:31 +02:00
GlobalStore.state.lastaction.type = 0
2019-04-27 20:12:41 +02:00
2019-04-29 01:01:31 +02:00
return await Mutations.mutations.movemyitem({ myitemorig: orig_obj, myitemdest: dest_obj })
}
2019-04-27 20:12:41 +02:00
}
}
}
2019-03-22 20:51:42 +01:00
export const actions = {
2019-03-28 12:58:34 +01:00
dbLoad: b.dispatch(dbLoad),
2021-02-11 02:21:36 +01:00
calculateHoursProjects: b.dispatch(calculateHoursProjects),
2019-03-22 20:51:42 +01:00
swapElems: b.dispatch(swapElems),
deleteItem: b.dispatch(deleteItem),
2019-03-28 12:58:34 +01:00
dbInsert: b.dispatch(dbInsert),
2019-04-27 20:12:41 +02:00
modify: b.dispatch(modify),
ActionCutPaste: b.dispatch(ActionCutPaste)
2019-03-22 20:51:42 +01:00
}
}
// Module
const ProjectsModule = {
get state() {
return stateGetter()
},
getters: Getters.getters,
mutations: Mutations.mutations,
actions: Actions.actions
}
export default ProjectsModule