2017-08-02 11:46:45 +01:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
|
|
|
// This software (Documize Community Edition) is licensed under
|
|
|
|
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
//
|
|
|
|
// You can operate outside the AGPL restrictions by purchasing
|
|
|
|
// Documize Enterprise Edition and obtaining a commercial license
|
|
|
|
// by contacting <sales@documize.com>.
|
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
2019-04-01 20:08:57 +01:00
|
|
|
"github.com/documize/community/model/category"
|
|
|
|
|
2017-08-02 11:46:45 +01:00
|
|
|
"github.com/documize/community/core/env"
|
|
|
|
"github.com/documize/community/core/event"
|
2022-03-16 17:39:01 -04:00
|
|
|
"github.com/documize/community/core/i18n"
|
2017-08-02 11:46:45 +01:00
|
|
|
"github.com/documize/community/core/request"
|
|
|
|
"github.com/documize/community/core/response"
|
|
|
|
"github.com/documize/community/core/secrets"
|
|
|
|
"github.com/documize/community/core/streamutil"
|
|
|
|
"github.com/documize/community/core/stringutil"
|
|
|
|
"github.com/documize/community/core/uniqueid"
|
|
|
|
"github.com/documize/community/domain"
|
2017-09-18 17:53:42 +01:00
|
|
|
"github.com/documize/community/domain/permission"
|
2017-08-15 19:41:44 +01:00
|
|
|
indexer "github.com/documize/community/domain/search"
|
2018-09-27 15:14:48 +01:00
|
|
|
"github.com/documize/community/domain/store"
|
2017-08-02 11:46:45 +01:00
|
|
|
"github.com/documize/community/model/attachment"
|
|
|
|
"github.com/documize/community/model/audit"
|
2019-11-15 14:51:52 +00:00
|
|
|
cm "github.com/documize/community/model/category"
|
2017-08-02 11:46:45 +01:00
|
|
|
"github.com/documize/community/model/doc"
|
|
|
|
"github.com/documize/community/model/page"
|
2017-09-18 17:53:42 +01:00
|
|
|
pm "github.com/documize/community/model/permission"
|
2019-04-01 20:08:57 +01:00
|
|
|
|
2018-12-17 13:39:13 +00:00
|
|
|
// "github.com/documize/community/model/template"
|
2018-03-14 13:38:37 +00:00
|
|
|
"github.com/documize/community/model/workflow"
|
2017-08-02 11:46:45 +01:00
|
|
|
uuid "github.com/nu7hatch/gouuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Handler contains the runtime information such as logging and database.
|
|
|
|
type Handler struct {
|
|
|
|
Runtime *env.Runtime
|
2018-09-27 15:14:48 +01:00
|
|
|
Store *store.Store
|
2017-08-15 19:41:44 +01:00
|
|
|
Indexer indexer.Indexer
|
2017-08-02 11:46:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SavedList returns all templates saved by the user
|
|
|
|
func (h *Handler) SavedList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "template.saved"
|
|
|
|
ctx := domain.GetRequestContext(r)
|
|
|
|
|
2018-10-12 17:54:15 +01:00
|
|
|
spaceID := request.Param(r, "spaceID")
|
|
|
|
if len(spaceID) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "spaceID")
|
2017-08-17 09:37:33 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:54:15 +01:00
|
|
|
documents, err := h.Store.Document.TemplatesBySpace(ctx, spaceID)
|
2017-08-02 11:46:45 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-17 13:39:13 +00:00
|
|
|
// templates := []template.Template{}
|
2017-08-02 11:46:45 +01:00
|
|
|
|
2018-12-17 13:39:13 +00:00
|
|
|
// for _, d := range documents {
|
|
|
|
// var t = template.Template{}
|
|
|
|
// t.ID = d.RefID
|
|
|
|
// t.Title = d.Name
|
|
|
|
// t.Description = d.Excerpt
|
|
|
|
// t.Author = ""
|
|
|
|
// t.Dated = d.Created
|
|
|
|
// t.Type = template.TypePrivate
|
2017-08-02 11:46:45 +01:00
|
|
|
|
2018-12-17 13:39:13 +00:00
|
|
|
// if d.SpaceID == spaceID {
|
|
|
|
// templates = append(templates, t)
|
|
|
|
// }
|
|
|
|
// }
|
2017-08-02 11:46:45 +01:00
|
|
|
|
2018-12-17 13:39:13 +00:00
|
|
|
response.WriteJSON(w, documents)
|
2017-08-02 11:46:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SaveAs saves existing document as a template.
|
|
|
|
func (h *Handler) SaveAs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "template.saved"
|
|
|
|
ctx := domain.GetRequestContext(r)
|
2022-03-21 19:14:10 -04:00
|
|
|
unseq := doc.Unsequenced
|
2017-08-02 11:46:45 +01:00
|
|
|
|
2018-11-05 19:48:50 +00:00
|
|
|
if !h.Runtime.Product.IsValid(ctx) {
|
2017-08-02 11:46:45 +01:00
|
|
|
response.WriteBadLicense(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var model struct {
|
|
|
|
DocumentID string
|
|
|
|
Name string
|
|
|
|
Excerpt string
|
|
|
|
}
|
|
|
|
|
|
|
|
defer streamutil.Close(r.Body)
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, "Bad payload")
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &model)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, "unmarshal")
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:58:33 +01:00
|
|
|
// Duplicate document
|
|
|
|
doc, err := h.Store.Document.Get(ctx, model.DocumentID)
|
2017-08-02 11:46:45 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-19 16:03:29 +01:00
|
|
|
if !permission.HasPermission(ctx, *h.Store, doc.SpaceID, pm.DocumentTemplate) {
|
2017-09-19 17:58:33 +01:00
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// DB transaction
|
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
2017-08-02 11:46:45 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
docID := uniqueid.Generate()
|
|
|
|
doc.Template = true
|
2018-09-19 16:03:29 +01:00
|
|
|
doc.Name = model.Name
|
2017-08-02 11:46:45 +01:00
|
|
|
doc.Excerpt = model.Excerpt
|
|
|
|
doc.RefID = docID
|
|
|
|
doc.ID = 0
|
|
|
|
doc.Template = true
|
2018-03-20 14:17:11 +00:00
|
|
|
doc.Lifecycle = workflow.LifecycleLive
|
2022-03-21 19:14:10 -04:00
|
|
|
doc.Sequence = unseq
|
2017-08-02 11:46:45 +01:00
|
|
|
|
|
|
|
// Duplicate pages and associated meta
|
|
|
|
pages, err := h.Store.Page.GetPages(ctx, model.DocumentID)
|
|
|
|
var pageModel []page.NewPage
|
|
|
|
|
|
|
|
if err != nil {
|
2018-02-04 15:43:57 +00:00
|
|
|
ctx.Transaction.Rollback()
|
2017-08-02 11:46:45 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range pages {
|
|
|
|
p.DocumentID = docID
|
|
|
|
p.ID = 0
|
|
|
|
|
|
|
|
meta, err2 := h.Store.Page.GetPageMeta(ctx, p.RefID)
|
|
|
|
if err2 != nil {
|
2018-02-04 15:43:57 +00:00
|
|
|
ctx.Transaction.Rollback()
|
2017-08-02 11:46:45 +01:00
|
|
|
response.WriteServerError(w, method, err2)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pageID := uniqueid.Generate()
|
|
|
|
p.RefID = pageID
|
2018-09-19 16:03:29 +01:00
|
|
|
meta.SectionID = pageID
|
2017-08-02 11:46:45 +01:00
|
|
|
meta.DocumentID = docID
|
|
|
|
|
|
|
|
m := page.NewPage{}
|
|
|
|
m.Page = p
|
|
|
|
m.Meta = meta
|
|
|
|
|
|
|
|
pageModel = append(pageModel, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duplicate attachments
|
|
|
|
attachments, _ := h.Store.Attachment.GetAttachments(ctx, model.DocumentID)
|
|
|
|
for i, a := range attachments {
|
|
|
|
a.DocumentID = docID
|
|
|
|
a.RefID = uniqueid.Generate()
|
|
|
|
a.ID = 0
|
|
|
|
attachments[i] = a
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now create the template: document, attachments, pages and their meta
|
|
|
|
err = h.Store.Document.Add(ctx, doc)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-18 17:57:17 -04:00
|
|
|
// Clone attachments.
|
2017-08-02 11:46:45 +01:00
|
|
|
for _, a := range attachments {
|
|
|
|
err = h.Store.Attachment.Add(ctx, a)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 17:57:17 -04:00
|
|
|
// Clone sections.
|
2017-08-02 11:46:45 +01:00
|
|
|
for _, m := range pageModel {
|
|
|
|
err = h.Store.Page.Add(ctx, m)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 17:57:17 -04:00
|
|
|
// Clone categories.
|
|
|
|
cats, err := h.Store.Category.GetDocumentCategoryMembership(ctx, model.DocumentID)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, c := range cats {
|
|
|
|
var cc category.Member
|
|
|
|
cc.OrgID = c.OrgID
|
|
|
|
cc.CategoryID = c.RefID
|
|
|
|
cc.RefID = uniqueid.Generate()
|
|
|
|
cc.DocumentID = docID
|
2018-09-19 16:03:29 +01:00
|
|
|
cc.SpaceID = doc.SpaceID
|
2018-07-18 17:57:17 -04:00
|
|
|
err = h.Store.Category.AssociateDocument(ctx, cc)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 11:46:45 +01:00
|
|
|
// Commit and return new document template
|
|
|
|
ctx.Transaction.Commit()
|
|
|
|
|
2019-09-24 13:39:57 +01:00
|
|
|
h.Store.Space.SetStats(ctx, doc.SpaceID)
|
2018-02-04 15:43:57 +00:00
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypeTemplateAdd)
|
|
|
|
|
2017-08-02 11:46:45 +01:00
|
|
|
doc, err = h.Store.Document.Get(ctx, docID)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response.WriteJSON(w, doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use creates new document using a saved document as a template.
|
|
|
|
// If template ID is ZERO then we provide an Empty Document as the new document.
|
|
|
|
func (h *Handler) Use(w http.ResponseWriter, r *http.Request) {
|
2018-09-27 15:14:48 +01:00
|
|
|
method := "template.use"
|
2017-08-02 11:46:45 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
|
|
|
|
2018-10-12 17:54:15 +01:00
|
|
|
spaceID := request.Param(r, "spaceID")
|
|
|
|
if len(spaceID) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "spaceID")
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
templateID := request.Param(r, "templateID")
|
|
|
|
if len(templateID) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "templateID")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer streamutil.Close(r.Body)
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, "Bad payload")
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
docTitle := string(body)
|
|
|
|
|
|
|
|
// Define an empty document just in case user wanted one.
|
|
|
|
var d = doc.Document{}
|
2018-09-19 16:03:29 +01:00
|
|
|
d.Name = docTitle
|
2017-08-02 11:46:45 +01:00
|
|
|
d.Location = fmt.Sprintf("template-%s", templateID)
|
2022-03-16 17:39:01 -04:00
|
|
|
d.Excerpt = i18n.Localize(ctx.Locale, "description")
|
2018-09-19 16:03:29 +01:00
|
|
|
d.Slug = stringutil.MakeSlug(d.Name)
|
2017-08-02 11:46:45 +01:00
|
|
|
d.Tags = ""
|
2018-10-12 17:54:15 +01:00
|
|
|
d.SpaceID = spaceID
|
2017-08-02 11:46:45 +01:00
|
|
|
documentID := uniqueid.Generate()
|
|
|
|
d.RefID = documentID
|
|
|
|
|
|
|
|
var pages = []page.Page{}
|
|
|
|
var attachments = []attachment.Attachment{}
|
|
|
|
|
|
|
|
// Fetch document and associated pages, attachments if we have template ID
|
|
|
|
if templateID != "0" {
|
|
|
|
d, err = h.Store.Document.Get(ctx, templateID)
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
response.WriteNotFoundError(w, method, templateID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pages, _ = h.Store.Page.GetPages(ctx, templateID)
|
|
|
|
attachments, _ = h.Store.Attachment.GetAttachmentsWithData(ctx, templateID)
|
|
|
|
}
|
|
|
|
|
2018-04-20 14:38:35 +01:00
|
|
|
// Fetch space where document resides.
|
2018-10-12 17:54:15 +01:00
|
|
|
sp, err := h.Store.Space.Get(ctx, spaceID)
|
2018-04-20 14:38:35 +01:00
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-01 20:08:57 +01:00
|
|
|
ctx.Transaction, _ = h.Runtime.StartTx(sql.LevelReadUncommitted)
|
2017-08-02 11:46:45 +01:00
|
|
|
|
|
|
|
// Prepare new document
|
|
|
|
documentID = uniqueid.Generate()
|
|
|
|
d.RefID = documentID
|
|
|
|
d.Template = false
|
2018-10-12 17:54:15 +01:00
|
|
|
d.SpaceID = spaceID
|
2017-08-02 11:46:45 +01:00
|
|
|
d.UserID = ctx.UserID
|
2018-09-19 16:03:29 +01:00
|
|
|
d.Name = docTitle
|
2022-03-21 19:14:10 -04:00
|
|
|
d.Sequence = doc.Unsequenced
|
2018-05-10 15:10:15 +01:00
|
|
|
|
2018-11-05 19:48:50 +00:00
|
|
|
if h.Runtime.Product.Edition == domain.CommunityEdition {
|
2018-05-10 15:10:15 +01:00
|
|
|
d.Lifecycle = workflow.LifecycleLive
|
|
|
|
} else {
|
|
|
|
d.Lifecycle = sp.Lifecycle
|
|
|
|
}
|
2017-08-02 11:46:45 +01:00
|
|
|
|
|
|
|
err = h.Store.Document.Add(ctx, d)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range pages {
|
|
|
|
meta, err2 := h.Store.Page.GetPageMeta(ctx, p.RefID)
|
|
|
|
if err2 != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.DocumentID = documentID
|
|
|
|
pageID := uniqueid.Generate()
|
|
|
|
p.RefID = pageID
|
|
|
|
|
2018-09-19 16:03:29 +01:00
|
|
|
meta.SectionID = pageID
|
2017-08-02 11:46:45 +01:00
|
|
|
meta.DocumentID = documentID
|
|
|
|
|
|
|
|
model := page.NewPage{}
|
|
|
|
model.Page = p
|
|
|
|
model.Meta = meta
|
|
|
|
|
|
|
|
err = h.Store.Page.Add(ctx, model)
|
|
|
|
if err != nil {
|
2019-04-01 20:08:57 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newUUID, _ := uuid.NewV4()
|
|
|
|
|
|
|
|
for _, a := range attachments {
|
|
|
|
a.DocumentID = documentID
|
|
|
|
a.Job = newUUID.String()
|
|
|
|
random := secrets.GenerateSalt()
|
|
|
|
a.FileID = random[0:9]
|
|
|
|
attachmentID := uniqueid.Generate()
|
|
|
|
a.RefID = attachmentID
|
|
|
|
|
|
|
|
err = h.Store.Attachment.Add(ctx, a)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 11:46:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 17:57:17 -04:00
|
|
|
// Clone categories.
|
|
|
|
cats, err := h.Store.Category.GetDocumentCategoryMembership(ctx, templateID)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2018-10-10 15:13:09 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2018-07-18 17:57:17 -04:00
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, c := range cats {
|
|
|
|
var cc category.Member
|
|
|
|
cc.OrgID = c.OrgID
|
|
|
|
cc.CategoryID = c.RefID
|
|
|
|
cc.RefID = uniqueid.Generate()
|
|
|
|
cc.DocumentID = d.RefID
|
2018-09-19 16:03:29 +01:00
|
|
|
cc.SpaceID = d.SpaceID
|
2018-07-18 17:57:17 -04:00
|
|
|
err = h.Store.Category.AssociateDocument(ctx, cc)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2018-10-10 15:13:09 +01:00
|
|
|
ctx.Transaction.Rollback()
|
2018-07-18 17:57:17 -04:00
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:51:52 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 11:46:45 +01:00
|
|
|
ctx.Transaction.Commit()
|
|
|
|
|
2019-09-24 13:39:57 +01:00
|
|
|
h.Store.Space.SetStats(ctx, d.SpaceID)
|
2018-02-04 15:43:57 +00:00
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypeTemplateUse)
|
|
|
|
|
2017-08-02 11:46:45 +01:00
|
|
|
nd, err := h.Store.Document.Get(ctx, documentID)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-19 16:03:29 +01:00
|
|
|
event.Handler().Publish(string(event.TypeAddDocument), nd.Name)
|
2017-08-02 11:46:45 +01:00
|
|
|
|
2017-08-15 19:41:44 +01:00
|
|
|
a, _ := h.Store.Attachment.GetAttachments(ctx, documentID)
|
2018-03-14 13:38:37 +00:00
|
|
|
|
|
|
|
if nd.Lifecycle == workflow.LifecycleLive {
|
|
|
|
go h.Indexer.IndexDocument(ctx, nd, a)
|
|
|
|
} else {
|
|
|
|
go h.Indexer.DeleteDocument(ctx, d.RefID)
|
|
|
|
}
|
2017-08-15 19:41:44 +01:00
|
|
|
|
2017-08-02 11:46:45 +01:00
|
|
|
response.WriteJSON(w, nd)
|
|
|
|
}
|