1- import { Bucket , listBuckets } from "@api/quantified-resume"
1+ import { Bucket , createBucket , listBuckets , updateBucket } from "@api/quantified-resume"
2+ import metaMessages , { } from "@i18n/message/common/meta"
3+ import { t } from "@i18n"
4+ import { groupBy } from "@util/array"
25
36export type QuantifiedResumeCache = {
47 bucketIds : {
@@ -7,6 +10,19 @@ export type QuantifiedResumeCache = {
710 }
811}
912
13+ async function createNewBucket ( context : timer . backup . CoordinatorContext < QuantifiedResumeCache > ) : Promise < number > {
14+ const { cid, cname } = context || { }
15+ const { endpoint } = context ?. ext || { }
16+ const appName = t ( metaMessages , { key : msg => msg . name } )
17+ const bucket : Bucket = {
18+ name : `${ appName } : ${ cid } ` ,
19+ builtin : "BrowserTime" ,
20+ builtinRefId : cid ,
21+ payload : { name : cname }
22+ }
23+ return createBucket ( { endpoint } , bucket )
24+ }
25+
1026async function getBucketId ( context : timer . backup . CoordinatorContext < QuantifiedResumeCache > ) : Promise < number > {
1127 const { cid, cache } = context || { }
1228 // 1. query from cache
@@ -16,25 +32,31 @@ async function getBucketId(context: timer.backup.CoordinatorContext<QuantifiedRe
1632 const { endpoint } = context ?. ext || { }
1733 // 2. query again
1834 bucketId = ( await listBuckets ( { endpoint } , cid ) ) ?. [ 0 ] ?. id
19- // TODO
2035 if ( ! bucketId ) {
2136 // 3. create one
22- const bucket : Bucket = {
23- name : "Time Tracker: " + cid ,
24- builtin : "BrowserTime" ,
25- builtinRefId : cid ,
26- payload : {
27- name : ""
28- }
29- }
37+ bucketId = await createNewBucket ( context )
3038 }
31- throw new Error ( "TODO" )
39+ return bucketId
3240}
3341
3442export default class QuantifiedResumeCoordinator implements timer . backup . Coordinator < QuantifiedResumeCache > {
35-
36- updateClients ( _ : timer . backup . CoordinatorContext < QuantifiedResumeCache > , clients : timer . backup . Client [ ] ) : Promise < void > {
37- throw new Error ( "Method not implemented." ) ;
43+ async updateClients ( context : timer . backup . CoordinatorContext < QuantifiedResumeCache > , clients : timer . backup . Client [ ] ) : Promise < void > {
44+ const { endpoint } = context ?. ext || { }
45+ const existBuckets = groupBy ( await listBuckets ( { endpoint } ) || [ ] , b => b . builtinRefId , l => l ?. [ 0 ] )
46+ if ( ! clients ?. length ) return
47+ const promises = Promise . all ( clients . map (
48+ async ( { id, name, minDate, maxDate } ) => {
49+ const exist = existBuckets [ id ]
50+ if ( exist ) {
51+ // update payload
52+ exist . payload = { name, minDate, maxDate }
53+ await updateBucket ( { endpoint } , exist )
54+ } else {
55+ await createNewBucket ( context )
56+ }
57+ } )
58+ )
59+ await promises
3860 }
3961
4062 async listAllClients ( context : timer . backup . CoordinatorContext < QuantifiedResumeCache > ) : Promise < timer . backup . Client [ ] > {
@@ -66,7 +88,9 @@ export default class QuantifiedResumeCoordinator implements timer.backup.Coordin
6688
6789 async upload ( context : timer . backup . CoordinatorContext < QuantifiedResumeCache > , rows : timer . stat . RowBase [ ] ) : Promise < void > {
6890 const bucketId = await getBucketId ( context )
69- throw new Error ( "Method not implemented." ) ;
91+ rows . forEach ( row => {
92+
93+ } )
7094 }
7195
7296 async testAuth ( _auth : timer . backup . Auth , ext : timer . backup . TypeExt ) : Promise < string > {
0 commit comments