endpoint.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. package fire
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "net/http"
  7. "strings"
  8. "cloud.google.com/go/firestore"
  9. firebase "firebase.google.com/go"
  10. )
  11. type onCall func(ctx context.Context, method string, ep *EndPoint) error
  12. type onLookup func(ctx context.Context, method string, ep *EndPoint) (map[string]interface{}, error)
  13. type onLoadUser func(client *firestore.Client, tx *firestore.Transaction, id string) (*User, error)
  14. // EndPoint defines the end point configuration
  15. type EndPoint struct {
  16. app *firebase.App // firebase app
  17. FirestoreClient *firestore.Client // firestore clinent
  18. Tx *firestore.Transaction // transaction
  19. // collection paths to create, update or delete
  20. CollectionPaths []string
  21. // doc paths to create, update or delete
  22. DocPaths []string
  23. // callback to load user
  24. OnLoadUser onLoadUser
  25. // callback invoked before updating documents
  26. Before onCall
  27. // callback invoked after updating documents
  28. After onCall
  29. // privileges of this endpoint
  30. Privileges []string
  31. // callback invoked between 'Before' and 'After' callbacks
  32. // it expects additional data for payload
  33. OnLookup onLookup
  34. // Payload to update document
  35. Payload *Payload
  36. // source is original document in 'PUT' and 'DELETE'
  37. source map[string]interface{}
  38. // user claims
  39. userclaim UserClaim
  40. }
  41. func NewEndPoint(app *firebase.App, colPaths []string, privileges []string, onLoadUser onLoadUser) *EndPoint {
  42. return &EndPoint{app: app, CollectionPaths: colPaths, Privileges: privileges, OnLoadUser: onLoadUser}
  43. }
  44. // // Fire has firebase variables
  45. // type Fire struct {
  46. // FirestoreClient *firestore.Client
  47. // Tx *firestore.Transaction
  48. // }
  49. func (r EndPoint) getUpdateFields() []firestore.Update {
  50. var fields []firestore.Update
  51. for k, v := range r.Payload.data {
  52. if k == "id" {
  53. continue
  54. }
  55. fields = append(fields, firestore.Update{Path: k, Value: v})
  56. }
  57. return fields
  58. }
  59. func (r EndPoint) getDeleteFields() []firestore.Update {
  60. var fields []firestore.Update
  61. fields = append(fields, firestore.Update{Path: "update_time", Value: r.Payload.Value("update_time")})
  62. fields = append(fields, firestore.Update{Path: "updated_by", Value: r.Payload.Value("updated_by")})
  63. fields = append(fields, firestore.Update{Path: "updated_by_id", Value: r.Payload.Value("updated_by_id")})
  64. fields = append(fields, firestore.Update{Path: "updated_date", Value: r.Payload.Value("updated_date")})
  65. fields = append(fields, firestore.Update{Path: "delete_time", Value: r.Payload.Value("delete_time")})
  66. return fields
  67. }
  68. func (r EndPoint) isDeleted() bool {
  69. if v, ok := r.source["delete_time"]; ok {
  70. var deleteTime int64
  71. switch value := v.(type) {
  72. case int:
  73. deleteTime = int64(value)
  74. case int64:
  75. deleteTime = value
  76. default:
  77. return false
  78. }
  79. return deleteTime > 0
  80. }
  81. return false
  82. }
  83. func (r EndPoint) substituteVar(ctx context.Context, p string) string {
  84. if strings.Index(p, "$") == 0 {
  85. key := p[1:]
  86. // looup from context
  87. _p, ok := getContextValue(ctx, key)
  88. if ok {
  89. return _p
  90. }
  91. // lookup from payload
  92. v := r.Payload.String(key)
  93. if v != "" {
  94. return _p
  95. }
  96. // return '/'
  97. return "/"
  98. }
  99. return p
  100. }
  101. func (r EndPoint) getPaths(ctx context.Context) []string {
  102. paths := make([]string, 0)
  103. for _, colPath := range r.CollectionPaths {
  104. path := ""
  105. ps := strings.Split(colPath, "/")
  106. for _, p := range ps {
  107. _p := r.substituteVar(ctx, p)
  108. if path == "" {
  109. path = _p
  110. } else {
  111. path = path + "/" + _p
  112. }
  113. }
  114. paths = append(paths, path)
  115. }
  116. return paths
  117. }
  118. // addPayloadTimeAndUser adds update time, update user
  119. func (rec *EndPoint) addPayloadTimeAndUser(client *firestore.Client, tx *firestore.Transaction, userID string, isDelete bool) error {
  120. now, _ := NowMM()
  121. sec := TimestampMilli(*now)
  122. _user, err := rec.OnLoadUser(client, tx, userID)
  123. if err != nil {
  124. return err
  125. }
  126. rec.Payload.data["update_time"] = sec
  127. rec.Payload.data["updated_by"] = _user.UserName
  128. rec.Payload.data["updated_by_id"] = userID
  129. rec.Payload.data["updated_date"] = now
  130. deleteTime := int64(0)
  131. if isDelete {
  132. deleteTime = sec
  133. }
  134. rec.Payload.data["delete_time"] = deleteTime
  135. return nil
  136. }
  137. func (ep *EndPoint) Post(ctx context.Context) (createdDoc map[string]interface{}, err error) {
  138. client, err := ep.app.Firestore(ctx)
  139. if err != nil {
  140. return nil, err
  141. }
  142. id, _ := ep.Payload.getID()
  143. refs := make([]*firestore.DocumentRef, 0)
  144. paths := ep.getPaths(ctx)
  145. for _, p := range paths {
  146. var ref *firestore.DocumentRef
  147. if id != "" {
  148. ref = ColRef(client, p).Doc(id)
  149. } else {
  150. ref = ColRef(client, p).NewDoc()
  151. }
  152. refs = append(refs, ref)
  153. }
  154. err = client.RunTransaction(ctx, func(ctx context.Context, tx *firestore.Transaction) error {
  155. err := ep.addPayloadTimeAndUser(client, tx, ep.userclaim.UserID, false)
  156. if err != nil {
  157. return err
  158. }
  159. ep.FirestoreClient = client
  160. ep.Tx = tx
  161. // fire := Fire{FirestoreClient: client, Tx: tx}
  162. if ep.Before != nil {
  163. err = ep.Before(ctx, "POST", ep)
  164. if err != nil {
  165. return err
  166. }
  167. }
  168. if ep.OnLookup != nil {
  169. data, err := ep.OnLookup(ctx, "POST", ep)
  170. if err != nil {
  171. return err
  172. }
  173. ep.Payload.Add(data)
  174. }
  175. for _, ref := range refs {
  176. if err := tx.Create(ref, ep.Payload.data); err != nil {
  177. return err
  178. }
  179. refLog := ref.Collection("logs").NewDoc()
  180. if err := tx.Create(refLog, ep.Payload); err != nil {
  181. return err
  182. }
  183. }
  184. if ep.After != nil {
  185. err = ep.After(ctx, "POST", ep)
  186. if err != nil {
  187. return err
  188. }
  189. }
  190. return nil
  191. })
  192. if err != nil {
  193. return nil, err
  194. }
  195. return getDocWithID(ctx, refs[0])
  196. }
  197. func (ep *EndPoint) Put(ctx context.Context) (map[string]interface{}, error) {
  198. id, err := ep.Payload.getID()
  199. if err != nil {
  200. return nil, err
  201. }
  202. client, err := ep.app.Firestore(ctx)
  203. if err != nil {
  204. return nil, err
  205. }
  206. refs := make([]*firestore.DocumentRef, 0)
  207. // collectin path
  208. paths := ep.getPaths(ctx)
  209. for _, p := range paths {
  210. ref := ColRef(client, p).Doc(id)
  211. refs = append(refs, ref)
  212. }
  213. // doc paths
  214. for _, p := range ep.DocPaths {
  215. ref := DocRef(client, p)
  216. refs = append(refs, ref)
  217. }
  218. err = client.RunTransaction(ctx, func(ctx context.Context, tx *firestore.Transaction) error {
  219. err := ep.addPayloadTimeAndUser(client, tx, ep.userclaim.UserID, false)
  220. if err != nil {
  221. return err
  222. }
  223. ep.FirestoreClient = client
  224. ep.Tx = tx
  225. if ep.Before != nil {
  226. err = ep.Before(ctx, "PUT", ep)
  227. if err != nil {
  228. return err
  229. }
  230. }
  231. if ep.OnLookup != nil {
  232. data, err := ep.OnLookup(ctx, "PUT", ep)
  233. if err != nil {
  234. return err
  235. }
  236. ep.Payload.Add(data)
  237. }
  238. snaps, err := tx.GetAll(refs)
  239. if err != nil {
  240. return err
  241. }
  242. for _, snap := range snaps {
  243. ep.source = snap.Data()
  244. if ep.isDeleted() {
  245. return errors.New("unable to update deleted data")
  246. }
  247. if err := tx.Update(snap.Ref, ep.getUpdateFields()); err != nil {
  248. return err
  249. }
  250. refLog := snap.Ref.Collection("logs").NewDoc()
  251. if err := tx.Create(refLog, ep.Payload); err != nil {
  252. return err
  253. }
  254. }
  255. if ep.After != nil {
  256. err = ep.After(ctx, "PUT", ep)
  257. return err
  258. }
  259. return nil
  260. })
  261. if err != nil {
  262. return nil, err
  263. }
  264. return getDocWithID(ctx, refs[0])
  265. }
  266. func (ep *EndPoint) Delete(ctx context.Context) (map[string]interface{}, error) {
  267. client, err := ep.app.Firestore(ctx)
  268. if err != nil {
  269. return nil, err
  270. }
  271. id, err := ep.Payload.getID()
  272. if err != nil {
  273. return nil, err
  274. }
  275. refs := make([]*firestore.DocumentRef, 0)
  276. // collection paths
  277. paths := ep.getPaths(ctx)
  278. for _, p := range paths {
  279. ref := ColRef(client, p).Doc(id)
  280. refs = append(refs, ref)
  281. }
  282. // doc paths
  283. for _, p := range ep.DocPaths {
  284. ref := DocRef(client, p)
  285. refs = append(refs, ref)
  286. }
  287. err = client.RunTransaction(ctx, func(ctx context.Context, tx *firestore.Transaction) error {
  288. err := ep.addPayloadTimeAndUser(client, tx, ep.userclaim.UserID, true)
  289. if err != nil {
  290. return err
  291. }
  292. ep.FirestoreClient = client
  293. ep.Tx = tx
  294. if ep.Before != nil {
  295. err = ep.Before(ctx, "DELETE", ep)
  296. if err != nil {
  297. return err
  298. }
  299. }
  300. if ep.OnLookup != nil {
  301. data, err := ep.OnLookup(ctx, "DELETE", ep)
  302. if err != nil {
  303. return err
  304. }
  305. ep.Payload.Add(data)
  306. }
  307. snaps, err := tx.GetAll(refs)
  308. if err != nil {
  309. return err
  310. }
  311. for _, snap := range snaps {
  312. ep.source = snap.Data()
  313. if ep.isDeleted() {
  314. return errors.New("already deleted")
  315. }
  316. if err := tx.Update(snap.Ref, ep.getDeleteFields()); err != nil {
  317. return err
  318. }
  319. refLog := snap.Ref.Collection("logs").NewDoc()
  320. if err := tx.Create(refLog, ep.Payload); err != nil {
  321. return err
  322. }
  323. }
  324. if ep.After != nil {
  325. err = ep.After(ctx, "DELETE", ep)
  326. return err
  327. }
  328. return err
  329. })
  330. if err != nil {
  331. return nil, err
  332. }
  333. return getDocWithID(ctx, refs[0])
  334. }
  335. // Load loads payload and userClaim into EndPoint
  336. // return error if no claim found or no privilege in the claim
  337. func (ep *EndPoint) Load(ctx context.Context, r *http.Request) error {
  338. userClaim, ok := GetUserClaim(ctx)
  339. if !ok {
  340. return errors.New("user claim not found")
  341. }
  342. if userClaim.UserID == "" {
  343. return errors.New("unidentified user id")
  344. }
  345. if len(ep.Privileges) > 0 {
  346. if has := userClaim.HasPrivileges(ep.Privileges...); !has {
  347. return errors.New("no privilege")
  348. }
  349. }
  350. var data map[string]interface{}
  351. decoder := json.NewDecoder(r.Body)
  352. err := decoder.Decode(&data)
  353. if err != nil {
  354. return err
  355. }
  356. ep.Payload = NewPayload(data)
  357. ep.userclaim = userClaim
  358. return nil
  359. }