2019-03-31 02:08:02 +00:00
|
|
|
namespace MyPrayerJournal
|
|
|
|
|
2019-07-14 03:55:53 +00:00
|
|
|
open System
|
|
|
|
open System.Collections.Generic
|
|
|
|
|
2019-07-29 03:21:05 +00:00
|
|
|
/// JSON converters for various DUs
|
|
|
|
module Converters =
|
|
|
|
|
2019-08-01 00:13:36 +00:00
|
|
|
open Microsoft.FSharpLu.Json
|
|
|
|
open Newtonsoft.Json
|
|
|
|
|
2019-07-29 03:21:05 +00:00
|
|
|
/// JSON converter for request IDs
|
|
|
|
type RequestIdJsonConverter () =
|
|
|
|
inherit JsonConverter<RequestId> ()
|
|
|
|
override __.WriteJson(writer : JsonWriter, value : RequestId, _ : JsonSerializer) =
|
|
|
|
(RequestId.toString >> writer.WriteValue) value
|
|
|
|
override __.ReadJson(reader: JsonReader, _ : Type, _ : RequestId, _ : bool, _ : JsonSerializer) =
|
|
|
|
(string >> RequestId.fromIdString) reader.Value
|
2019-07-14 03:55:53 +00:00
|
|
|
|
2019-07-29 03:21:05 +00:00
|
|
|
/// JSON converter for user IDs
|
|
|
|
type UserIdJsonConverter () =
|
|
|
|
inherit JsonConverter<UserId> ()
|
|
|
|
override __.WriteJson(writer : JsonWriter, value : UserId, _ : JsonSerializer) =
|
|
|
|
(UserId.toString >> writer.WriteValue) value
|
|
|
|
override __.ReadJson(reader: JsonReader, _ : Type, _ : UserId, _ : bool, _ : JsonSerializer) =
|
|
|
|
(string >> UserId) reader.Value
|
2019-07-14 03:55:53 +00:00
|
|
|
|
2019-07-29 03:21:05 +00:00
|
|
|
/// JSON converter for Ticks
|
|
|
|
type TicksJsonConverter () =
|
|
|
|
inherit JsonConverter<Ticks> ()
|
|
|
|
override __.WriteJson(writer : JsonWriter, value : Ticks, _ : JsonSerializer) =
|
|
|
|
(Ticks.toLong >> writer.WriteValue) value
|
|
|
|
override __.ReadJson(reader: JsonReader, _ : Type, _ : Ticks, _ : bool, _ : JsonSerializer) =
|
|
|
|
(string >> int64 >> Ticks) reader.Value
|
2019-07-14 03:55:53 +00:00
|
|
|
|
2019-08-01 00:13:36 +00:00
|
|
|
/// A sequence of all custom converters needed for myPrayerJournal
|
2019-07-29 03:21:05 +00:00
|
|
|
let all : JsonConverter seq =
|
|
|
|
seq {
|
|
|
|
yield RequestIdJsonConverter ()
|
|
|
|
yield UserIdJsonConverter ()
|
|
|
|
yield TicksJsonConverter ()
|
2019-08-01 00:13:36 +00:00
|
|
|
yield CompactUnionJsonConverter true
|
2019-07-29 03:21:05 +00:00
|
|
|
}
|
2019-07-14 03:55:53 +00:00
|
|
|
|
2019-08-01 00:13:36 +00:00
|
|
|
|
2019-07-29 03:21:05 +00:00
|
|
|
/// RavenDB index declarations
|
|
|
|
module Indexes =
|
|
|
|
|
2019-08-01 00:13:36 +00:00
|
|
|
open Raven.Client.Documents.Indexes
|
2019-07-14 03:55:53 +00:00
|
|
|
|
2019-07-29 03:21:05 +00:00
|
|
|
/// Index requests for a journal view
|
|
|
|
type Requests_AsJournal () as this =
|
|
|
|
inherit AbstractJavaScriptIndexCreationTask ()
|
|
|
|
do
|
|
|
|
this.Maps <- HashSet<string> [
|
2019-08-01 00:13:36 +00:00
|
|
|
"""docs.Requests.Select(req => new {
|
|
|
|
requestId = req.Id.Replace("Requests/", ""),
|
2019-07-29 03:21:05 +00:00
|
|
|
userId = req.userId,
|
|
|
|
text = req.history.Where(hist => hist.text != null).OrderByDescending(hist => hist.asOf).First().text,
|
|
|
|
asOf = req.history.OrderByDescending(hist => hist.asOf).First().asOf,
|
2019-07-31 04:56:34 +00:00
|
|
|
lastStatus = req.history.OrderByDescending(hist => hist.asOf).First().status,
|
2019-07-29 03:21:05 +00:00
|
|
|
snoozedUntil = req.snoozedUntil,
|
|
|
|
showAfter = req.showAfter,
|
|
|
|
recurType = req.recurType,
|
|
|
|
recurCount = req.recurCount
|
2019-08-01 00:13:36 +00:00
|
|
|
})"""
|
2019-07-29 03:21:05 +00:00
|
|
|
]
|
|
|
|
this.Fields <-
|
2019-08-01 00:13:36 +00:00
|
|
|
[ "requestId", IndexFieldOptions (Storage = Nullable FieldStorage.Yes)
|
|
|
|
"text", IndexFieldOptions (Storage = Nullable FieldStorage.Yes)
|
2019-07-31 04:56:34 +00:00
|
|
|
"asOf", IndexFieldOptions (Storage = Nullable FieldStorage.Yes)
|
|
|
|
"lastStatus", IndexFieldOptions (Storage = Nullable FieldStorage.Yes)
|
2019-07-29 03:21:05 +00:00
|
|
|
]
|
|
|
|
|> dict
|
|
|
|
|> Dictionary<string, IndexFieldOptions>
|
2019-07-14 03:55:53 +00:00
|
|
|
|
|
|
|
|
2019-07-31 04:56:34 +00:00
|
|
|
/// All data manipulations within myPrayerJournal
|
|
|
|
module Data =
|
2019-03-31 02:08:02 +00:00
|
|
|
|
2019-08-01 00:13:36 +00:00
|
|
|
open FSharp.Control.Tasks.V2.ContextInsensitive
|
2019-07-31 04:56:34 +00:00
|
|
|
open Indexes
|
2019-08-01 00:13:36 +00:00
|
|
|
open Microsoft.FSharpLu
|
|
|
|
open Raven.Client.Documents
|
|
|
|
open Raven.Client.Documents.Linq
|
2019-07-31 04:56:34 +00:00
|
|
|
open Raven.Client.Documents.Session
|
2019-03-31 02:08:02 +00:00
|
|
|
|
2019-07-31 04:56:34 +00:00
|
|
|
/// Add a history entry
|
|
|
|
let addHistory reqId (hist : History) (sess : IAsyncDocumentSession) =
|
|
|
|
sess.Advanced.Patch<Request, History> (
|
|
|
|
RequestId.toString reqId,
|
|
|
|
(fun r -> r.history :> IEnumerable<History>),
|
|
|
|
fun (h : JavaScriptArray<History>) -> h.Add (hist) :> obj)
|
2019-03-31 02:08:02 +00:00
|
|
|
|
2019-07-31 04:56:34 +00:00
|
|
|
/// Add a note
|
|
|
|
let addNote reqId (note : Note) (sess : IAsyncDocumentSession) =
|
|
|
|
sess.Advanced.Patch<Request, Note> (
|
|
|
|
RequestId.toString reqId,
|
|
|
|
(fun r -> r.notes :> IEnumerable<Note>),
|
|
|
|
fun (h : JavaScriptArray<Note>) -> h.Add (note) :> obj)
|
2019-03-31 02:08:02 +00:00
|
|
|
|
2019-07-31 04:56:34 +00:00
|
|
|
/// Add a request
|
|
|
|
let addRequest req (sess : IAsyncDocumentSession) =
|
|
|
|
sess.StoreAsync (req, req.Id)
|
2019-03-31 02:08:02 +00:00
|
|
|
|
|
|
|
/// Retrieve all answered requests for the given user
|
2019-07-31 04:56:34 +00:00
|
|
|
let answeredRequests userId (sess : IAsyncDocumentSession) =
|
2019-08-01 00:13:36 +00:00
|
|
|
task {
|
|
|
|
let! reqs =
|
|
|
|
sess.Query<JournalRequest, Requests_AsJournal>()
|
|
|
|
.Where(fun r -> r.userId = userId && r.lastStatus = "Answered")
|
|
|
|
.OrderByDescending(fun r -> r.asOf)
|
|
|
|
.ProjectInto<JournalRequest>()
|
|
|
|
.ToListAsync ()
|
|
|
|
return List.ofSeq reqs
|
|
|
|
}
|
2019-07-31 04:56:34 +00:00
|
|
|
|
2019-03-31 02:08:02 +00:00
|
|
|
/// Retrieve the user's current journal
|
2019-07-31 04:56:34 +00:00
|
|
|
let journalByUserId userId (sess : IAsyncDocumentSession) =
|
2019-03-31 02:08:02 +00:00
|
|
|
task {
|
2019-07-31 04:56:34 +00:00
|
|
|
let! jrnl =
|
|
|
|
sess.Query<JournalRequest, Requests_AsJournal>()
|
|
|
|
.Where(fun r -> r.userId = userId && r.lastStatus <> "Answered")
|
|
|
|
.OrderBy(fun r -> r.asOf)
|
|
|
|
.ProjectInto<JournalRequest>()
|
|
|
|
.ToListAsync()
|
|
|
|
return
|
|
|
|
jrnl
|
|
|
|
|> List.ofSeq
|
|
|
|
|> List.map (fun r -> r.history <- []; r.notes <- []; r)
|
2019-03-31 02:08:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 04:56:34 +00:00
|
|
|
/// Save changes in the current document session
|
|
|
|
let saveChanges (sess : IAsyncDocumentSession) =
|
|
|
|
sess.SaveChangesAsync ()
|
|
|
|
|
|
|
|
/// Retrieve a request, including its history and notes, by its ID and user ID
|
|
|
|
let tryFullRequestById reqId userId (sess : IAsyncDocumentSession) =
|
2019-03-31 02:08:02 +00:00
|
|
|
task {
|
2019-07-31 04:56:34 +00:00
|
|
|
let! req = RequestId.toString reqId |> sess.LoadAsync
|
|
|
|
return match Option.fromObject req with Some r when r.userId = userId -> Some r | _ -> None
|
2019-03-31 02:08:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 04:56:34 +00:00
|
|
|
|
|
|
|
/// Retrieve a request by its ID and user ID (without notes and history)
|
|
|
|
let tryRequestById reqId userId (sess : IAsyncDocumentSession) =
|
2019-03-31 02:08:02 +00:00
|
|
|
task {
|
2019-07-31 04:56:34 +00:00
|
|
|
match! tryFullRequestById reqId userId sess with
|
|
|
|
| Some r -> return Some { r with history = []; notes = [] }
|
|
|
|
| _ -> return None
|
2019-03-31 02:08:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 04:56:34 +00:00
|
|
|
/// Retrieve notes for a request by its ID and user ID
|
|
|
|
let notesById reqId userId (sess : IAsyncDocumentSession) =
|
2019-03-31 02:08:02 +00:00
|
|
|
task {
|
2019-07-31 04:56:34 +00:00
|
|
|
match! tryFullRequestById reqId userId sess with
|
|
|
|
| Some req -> return req.notes
|
|
|
|
| None -> return []
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve a journal request by its ID and user ID
|
|
|
|
let tryJournalById reqId userId (sess : IAsyncDocumentSession) =
|
|
|
|
task {
|
|
|
|
let! req =
|
|
|
|
sess.Query<Request, Requests_AsJournal>()
|
|
|
|
.Where(fun x -> x.Id = (RequestId.toString reqId) && x.userId = userId)
|
|
|
|
.ProjectInto<JournalRequest>()
|
|
|
|
.FirstOrDefaultAsync ()
|
|
|
|
return Option.fromObject req
|
2019-03-31 02:08:02 +00:00
|
|
|
}
|
2019-07-31 04:56:34 +00:00
|
|
|
|
|
|
|
/// Update the recurrence for a request
|
|
|
|
let updateRecurrence reqId recurType recurCount (sess : IAsyncDocumentSession) =
|
|
|
|
sess.Advanced.Patch<Request, Recurrence> (RequestId.toString reqId, (fun r -> r.recurType), recurType)
|
|
|
|
sess.Advanced.Patch<Request, int16> (RequestId.toString reqId, (fun r -> r.recurCount), recurCount)
|
|
|
|
|
|
|
|
/// Update a snoozed request
|
|
|
|
let updateSnoozed reqId until (sess : IAsyncDocumentSession) =
|
|
|
|
sess.Advanced.Patch<Request, Ticks> (RequestId.toString reqId, (fun r -> r.snoozedUntil), until)
|
|
|
|
sess.Advanced.Patch<Request, Ticks> (RequestId.toString reqId, (fun r -> r.showAfter), until)
|
|
|
|
|
|
|
|
/// Update the "show after" timestamp for a request
|
|
|
|
let updateShowAfter reqId showAfter (sess : IAsyncDocumentSession) =
|
|
|
|
sess.Advanced.Patch<Request, Ticks> (RequestId.toString reqId, (fun r -> r.showAfter), showAfter)
|