ingest documents
This commit is contained in:
@@ -131,3 +131,82 @@ func (s *Service) CreateFolder(ctx context.Context, actorID ident.UUID, params C
|
||||
|
||||
return documentFromDB(created), nil
|
||||
}
|
||||
|
||||
func (s *Service) CreateChild(ctx context.Context, actorID ident.UUID, params CreateChildParams) (*Document, error) {
|
||||
if err := s.requireActive(ctx, actorID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if params.OwnerID != actorID || params.CreatedBy != actorID {
|
||||
return nil, fmt.Errorf("%w: owner_id et created_by doivent être l'acteur", ErrPermissionDenied)
|
||||
}
|
||||
|
||||
name, nameKey, err := normalizeName(params.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
parent, err := s.getDocument(ctx, params.ParentID)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrDocumentNotFound) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrParentNotFound, params.ParentID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if parent.DeletedAt.Valid {
|
||||
return nil, fmt.Errorf("%w: %s", ErrParentDeleted, params.ParentID)
|
||||
}
|
||||
if !isContainerKind(parent.Kind) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrInvalidParent, parent.Kind)
|
||||
}
|
||||
if err := s.can(ctx, actorID, params.ParentID, permission.Create); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
kind, err := s.db.Queries.GetDocumentKind(ctx, params.Kind)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrInvalidKind, params.Kind)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if kind.Kind == KindLibrary || kind.Hidden != 0 {
|
||||
return nil, fmt.Errorf("%w: %s", ErrInvalidKind, params.Kind)
|
||||
}
|
||||
|
||||
metadata, err := normalizeMetadata(params.Metadata)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id, err := ident.NewV7()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := nowMillis()
|
||||
|
||||
created, err := s.db.WriteQueries.CreateDocument(ctx, database.CreateDocumentParams{
|
||||
ID: id,
|
||||
Kind: kind.Kind,
|
||||
OwnerID: params.OwnerID,
|
||||
CreatedBy: params.CreatedBy,
|
||||
ParentID: ¶ms.ParentID,
|
||||
Name: name,
|
||||
NameKey: nameKey,
|
||||
Path: parent.Path + hex.EncodeToString(id[:]) + "/",
|
||||
Depth: parent.Depth + 1,
|
||||
Metadata: metadata,
|
||||
AclInherit: aclInheritValue(params.ACLInherit),
|
||||
Rev: 1,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
})
|
||||
if err != nil {
|
||||
if isUniqueViolation(err) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrNameTaken, name)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return documentFromDB(created), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user