mirror of
https://github.com/documize/community.git
synced 2025-07-19 05:09:42 +02:00
Specify default categories for new documents
Closes #339 All new documents will be assigned default categories. Documents created from templates that already have categories take precedence.
This commit is contained in:
parent
5c1ad25dc9
commit
f117e91bcb
18 changed files with 1284 additions and 1134 deletions
|
@ -545,7 +545,8 @@ func (b backerHandler) dmzCategory(files *[]backupItem) (err error) {
|
|||
err = b.Runtime.Db.Select(&cat, `
|
||||
SELECT id, c_refid AS refid,
|
||||
c_orgid AS orgid, c_spaceid AS spaceid,
|
||||
c_name AS name, c_created AS created, c_revised AS revised
|
||||
c_name AS name, c_default AS isdefault,
|
||||
c_created AS created, c_revised AS revised
|
||||
FROM dmz_category`+w)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "select.category")
|
||||
|
|
|
@ -743,9 +743,9 @@ func (r *restoreHandler) dmzCategory() (err error) {
|
|||
|
||||
for i := range ct {
|
||||
_, err = r.Context.Transaction.Exec(r.Runtime.Db.Rebind(`
|
||||
INSERT INTO dmz_category (c_refid, c_orgid, c_spaceid, c_name, c_created, c_revised)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`),
|
||||
ct[i].RefID, r.remapOrg(ct[i].OrgID), ct[i].SpaceID, ct[i].Name, ct[i].Created, ct[i].Revised)
|
||||
INSERT INTO dmz_category (c_refid, c_orgid, c_spaceid, c_name, c_default, c_created, c_revised)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?`),
|
||||
ct[i].RefID, r.remapOrg(ct[i].OrgID), ct[i].SpaceID, ct[i].Name, ct[i].IsDefault, ct[i].Created, ct[i].Revised)
|
||||
|
||||
if err != nil {
|
||||
r.Context.Transaction.Rollback()
|
||||
|
|
|
@ -33,8 +33,8 @@ func (s Store) Add(ctx domain.RequestContext, c category.Category) (err error) {
|
|||
c.Created = time.Now().UTC()
|
||||
c.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_category (c_refid, c_orgid, c_spaceid, c_name, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?)"),
|
||||
c.RefID, c.OrgID, c.SpaceID, c.Name, c.Created, c.Revised)
|
||||
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_category (c_refid, c_orgid, c_spaceid, c_name, c_default, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?)"),
|
||||
c.RefID, c.OrgID, c.SpaceID, c.Name, c.IsDefault, c.Created, c.Revised)
|
||||
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "unable to execute insert category")
|
||||
|
@ -47,7 +47,7 @@ func (s Store) Add(ctx domain.RequestContext, c category.Category) (err error) {
|
|||
// Context is used to for user ID.
|
||||
func (s Store) GetBySpace(ctx domain.RequestContext, spaceID string) (c []category.Category, err error) {
|
||||
err = s.Runtime.Db.Select(&c, s.Bind(`
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_created AS created, c_revised AS revised
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_default AS isdefault, c_created AS created, c_revised AS revised
|
||||
FROM dmz_category
|
||||
WHERE c_orgid=? AND c_spaceid=? AND c_refid IN
|
||||
(
|
||||
|
@ -77,7 +77,7 @@ func (s Store) GetAllBySpace(ctx domain.RequestContext, spaceID string) (c []cat
|
|||
c = []category.Category{}
|
||||
|
||||
err = s.Runtime.Db.Select(&c, s.Bind(`
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_created AS created, c_revised AS revised
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_default AS isdefault, c_created AS created, c_revised AS revised
|
||||
FROM dmz_category
|
||||
WHERE c_orgid=? AND c_spaceid=? AND c_spaceid IN
|
||||
(
|
||||
|
@ -105,7 +105,7 @@ func (s Store) GetAllBySpace(ctx domain.RequestContext, spaceID string) (c []cat
|
|||
// GetByOrg returns all categories accessible by user for their org.
|
||||
func (s Store) GetByOrg(ctx domain.RequestContext, userID string) (c []category.Category, err error) {
|
||||
err = s.Runtime.Db.Select(&c, s.Bind(`
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_created AS created, c_revised AS revised
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_default AS isdefault, c_created AS created, c_revised AS revised
|
||||
FROM dmz_category
|
||||
WHERE c_orgid=? AND c_refid IN
|
||||
(SELECT c_refid FROM dmz_permission WHERE c_orgid=? AND c_location='category' AND c_refid IN (
|
||||
|
@ -131,7 +131,7 @@ func (s Store) GetByOrg(ctx domain.RequestContext, userID string) (c []category.
|
|||
func (s Store) Update(ctx domain.RequestContext, c category.Category) (err error) {
|
||||
c.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.NamedExec(s.Bind("UPDATE dmz_category SET c_name=:name, c_revised=:revised WHERE c_orgid=:orgid AND c_refid=:refid"), c)
|
||||
_, err = ctx.Transaction.NamedExec(s.Bind("UPDATE dmz_category SET c_name=:name, c_default=:isdefault, c_revised=:revised WHERE c_orgid=:orgid AND c_refid=:refid"), c)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("unable to execute update for category %s", c.RefID))
|
||||
}
|
||||
|
@ -142,7 +142,7 @@ func (s Store) Update(ctx domain.RequestContext, c category.Category) (err error
|
|||
// Get returns specified category
|
||||
func (s Store) Get(ctx domain.RequestContext, id string) (c category.Category, err error) {
|
||||
err = s.Runtime.Db.Get(&c, s.Bind(`
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_created AS created, c_revised AS revised
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_default AS isdefault, c_created AS created, c_revised AS revised
|
||||
FROM dmz_category
|
||||
WHERE c_orgid=? AND c_refid=?`),
|
||||
ctx.OrgID, id)
|
||||
|
@ -263,7 +263,7 @@ func (s Store) GetSpaceCategorySummary(ctx domain.RequestContext, spaceID string
|
|||
// GetDocumentCategoryMembership returns all space categories associated with given document.
|
||||
func (s Store) GetDocumentCategoryMembership(ctx domain.RequestContext, documentID string) (c []category.Category, err error) {
|
||||
err = s.Runtime.Db.Select(&c, s.Bind(`
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_created AS created, c_revised AS revised
|
||||
SELECT id, c_refid AS refid, c_orgid AS orgid, c_spaceid AS spaceid, c_name AS name, c_default AS isdefault, c_created AS created, c_revised AS revised
|
||||
FROM dmz_category
|
||||
WHERE c_orgid=? AND c_refid IN (SELECT c_categoryid FROM dmz_category_member WHERE c_orgid=? AND c_docid=?)`),
|
||||
ctx.OrgID, ctx.OrgID, documentID)
|
||||
|
|
|
@ -34,6 +34,7 @@ import (
|
|||
"github.com/documize/community/model/activity"
|
||||
"github.com/documize/community/model/attachment"
|
||||
"github.com/documize/community/model/audit"
|
||||
cm "github.com/documize/community/model/category"
|
||||
"github.com/documize/community/model/doc"
|
||||
"github.com/documize/community/model/page"
|
||||
"github.com/documize/community/model/space"
|
||||
|
@ -165,7 +166,8 @@ func (h *Handler) convert(w http.ResponseWriter, r *http.Request, job, spaceID s
|
|||
response.WriteJSON(w, nd)
|
||||
}
|
||||
|
||||
func processDocument(ctx domain.RequestContext, r *env.Runtime, store *store.Store, indexer indexer.Indexer, filename, job string, sp space.Space, fileResult *api.DocumentConversionResponse) (newDocument doc.Document, err error) {
|
||||
func processDocument(ctx domain.RequestContext, r *env.Runtime, store *store.Store, indexer indexer.Indexer, filename,
|
||||
job string, sp space.Space, fileResult *api.DocumentConversionResponse) (newDocument doc.Document, err error) {
|
||||
// Convert into database objects
|
||||
document := convertFileResult(filename, fileResult)
|
||||
document.Job = job
|
||||
|
@ -243,9 +245,30 @@ func processDocument(ctx domain.RequestContext, r *env.Runtime, store *store.Sto
|
|||
da = append(da, a)
|
||||
}
|
||||
|
||||
// Add default categories to newly created document (if we have them).
|
||||
cats, err := store.Category.GetBySpace(ctx, document.SpaceID)
|
||||
if err != nil {
|
||||
r.Log.Error("fetch default categories for new document", err)
|
||||
}
|
||||
for ic := range cats {
|
||||
if cats[ic].IsDefault {
|
||||
c := cm.Member{}
|
||||
c.OrgID = ctx.OrgID
|
||||
c.SpaceID = sp.RefID
|
||||
c.RefID = uniqueid.Generate()
|
||||
c.DocumentID = document.RefID
|
||||
c.CategoryID = cats[ic].RefID
|
||||
|
||||
err = store.Category.AssociateDocument(ctx, c)
|
||||
if err != nil {
|
||||
r.Log.Error("apply default category to new document", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
store.Activity.RecordUserActivity(ctx, activity.UserActivity{
|
||||
SpaceID: newDocument.SpaceID,
|
||||
DocumentID: newDocument.RefID,
|
||||
SpaceID: document.SpaceID,
|
||||
DocumentID: document.RefID,
|
||||
SourceType: activity.SourceTypeDocument,
|
||||
ActivityType: activity.TypeCreated})
|
||||
|
||||
|
|
|
@ -204,12 +204,13 @@ func (h *Handler) processSampleData(data om.SampleData) (err error) {
|
|||
h.Runtime.Log.Info(fmt.Sprintf("Installing (%d) categories", len(data.Category)))
|
||||
for i := range data.Category {
|
||||
_, err = data.Context.Transaction.Exec(h.Runtime.Db.Rebind(`
|
||||
INSERT INTO dmz_category (c_refid, c_orgid, c_spaceid, c_name, c_created, c_revised)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`),
|
||||
INSERT INTO dmz_category (c_refid, c_orgid, c_spaceid, c_name, c_default, c_created, c_revised)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`),
|
||||
h.getMappedID("category", data.Category[i].RefID),
|
||||
data.Context.OrgID,
|
||||
h.getMappedID("space", data.Category[i].SpaceID),
|
||||
data.Category[i].Name,
|
||||
data.Category[i].IsDefault,
|
||||
data.Category[i].Created,
|
||||
data.Category[i].Revised)
|
||||
|
||||
|
|
|
@ -34,6 +34,7 @@ import (
|
|||
"github.com/documize/community/domain/store"
|
||||
"github.com/documize/community/model/attachment"
|
||||
"github.com/documize/community/model/audit"
|
||||
cm "github.com/documize/community/model/category"
|
||||
"github.com/documize/community/model/doc"
|
||||
"github.com/documize/community/model/page"
|
||||
pm "github.com/documize/community/model/permission"
|
||||
|
@ -427,6 +428,30 @@ func (h *Handler) Use(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
// If document has no categories then we use
|
||||
// default categories for the space.
|
||||
if len(cats) == 0 {
|
||||
cats, err = h.Store.Category.GetBySpace(ctx, d.SpaceID)
|
||||
if err != nil {
|
||||
h.Runtime.Log.Error("fetch default categories for new document", err)
|
||||
}
|
||||
for ic := range cats {
|
||||
if cats[ic].IsDefault {
|
||||
c := cm.Member{}
|
||||
c.OrgID = ctx.OrgID
|
||||
c.SpaceID = d.SpaceID
|
||||
c.RefID = uniqueid.Generate()
|
||||
c.DocumentID = d.RefID
|
||||
c.CategoryID = cats[ic].RefID
|
||||
|
||||
err = h.Store.Category.AssociateDocument(ctx, c)
|
||||
if err != nil {
|
||||
h.Runtime.Log.Error("apply default category to new document", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
h.Store.Space.SetStats(ctx, d.SpaceID)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue