View | Details | Raw Unified | Return to bug 32939
Collapse All | Expand All

(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementRelationships.vue (-2 / +3 lines)
Lines 85-91 Link Here
85
</template>
85
</template>
86
86
87
<script>
87
<script>
88
import { fetchAgreements } from "../../fetch/erm.js"
88
import { ERMAPIClient } from "../../fetch/erm-api-client.js";
89
89
90
export default {
90
export default {
91
    data() {
91
    data() {
Lines 94-100 export default { Link Here
94
        }
94
        }
95
    },
95
    },
96
    beforeCreate() {
96
    beforeCreate() {
97
        fetchAgreements().then(agreements => {
97
        const client = new ERMAPIClient()
98
        const agreements = client.agreements.getAll().then(agreements => {
98
            this.agreements = agreements.filter(
99
            this.agreements = agreements.filter(
99
                agreement => agreement.agreement_id !== this.agreement_id
100
                agreement => agreement.agreement_id !== this.agreement_id
100
            )
101
            )
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue (-34 / +22 lines)
Lines 190-196 import AgreementLicenses from "./AgreementLicenses.vue" Link Here
190
import AgreementRelationships from "./AgreementRelationships.vue"
190
import AgreementRelationships from "./AgreementRelationships.vue"
191
import Documents from "./Documents.vue"
191
import Documents from "./Documents.vue"
192
import { setMessage, setError, setWarning } from "../../messages"
192
import { setMessage, setError, setWarning } from "../../messages"
193
import { fetchAgreement } from "../../fetch/erm.js"
193
import { ERMAPIClient } from "../../fetch/erm-api-client.js";
194
import { storeToRefs } from "pinia"
194
import { storeToRefs } from "pinia"
195
195
196
export default {
196
export default {
Lines 245-251 export default { Link Here
245
    beforeRouteEnter(to, from, next) {
245
    beforeRouteEnter(to, from, next) {
246
        next(vm => {
246
        next(vm => {
247
            if (to.params.agreement_id) {
247
            if (to.params.agreement_id) {
248
                vm.agreement = vm.getAgreement(to.params.agreement_id)
248
                vm.getAgreement(to.params.agreement_id)
249
            } else {
249
            } else {
250
                vm.initialized = true
250
                vm.initialized = true
251
            }
251
            }
Lines 253-261 export default { Link Here
253
    },
253
    },
254
    methods: {
254
    methods: {
255
        async getAgreement(agreement_id) {
255
        async getAgreement(agreement_id) {
256
            const agreement = await fetchAgreement(agreement_id)
256
            const client = new ERMAPIClient();
257
            this.agreement = agreement
257
            try {
258
            this.initialized = true
258
                await client.agreements.get(agreement_id).then(data => { this.agreement = data; this.initialized = true; } );
259
            } catch (err) {
260
                setError(err.message || err.statusText)
261
            }
259
        },
262
        },
260
        checkForm(agreement) {
263
        checkForm(agreement) {
261
            let errors = []
264
            let errors = []
Lines 326-343 export default { Link Here
326
329
327
            //let agreement= Object.assign( {} ,this.agreement); // copy
330
            //let agreement= Object.assign( {} ,this.agreement); // copy
328
            let agreement = JSON.parse(JSON.stringify(this.agreement)) // copy
331
            let agreement = JSON.parse(JSON.stringify(this.agreement)) // copy
332
            let agreement_id = agreement.agreement_id;
329
333
330
            if (!this.checkForm(agreement)) {
334
            if (!this.checkForm(agreement)) {
331
                return false
335
                return false
332
            }
336
            }
333
337
334
            let apiUrl = "/api/v1/erm/agreements"
335
336
            let method = "POST"
337
            if (agreement.agreement_id) {
338
                method = "PUT"
339
                apiUrl += "/" + agreement.agreement_id
340
            }
341
            delete agreement.agreement_id
338
            delete agreement.agreement_id
342
            delete agreement.vendor
339
            delete agreement.vendor
343
            agreement.is_perpetual = agreement.is_perpetual ? true : false
340
            agreement.is_perpetual = agreement.is_perpetual ? true : false
Lines 375-403 export default { Link Here
375
372
376
            delete agreement.agreement_packages
373
            delete agreement.agreement_packages
377
374
378
            const options = {
375
            const client = new ERMAPIClient();
379
                method: method,
376
            (async () => {
380
                body: JSON.stringify(agreement),
377
                try {
381
                headers: {
378
                    if ( agreement_id ) {
382
                    "Content-Type": "application/json;charset=utf-8",
379
                        await client.agreements.update(agreement, agreement_id).then(setMessage(this.$__("Agreement updated")));
383
                },
380
                    }else{
384
            }
381
                        await client.agreements.create(agreement).then(setMessage(this.$__("Agreement created")));
385
386
            fetch(apiUrl, options)
387
                .then(response => {
388
                    if (response.status == 200) {
389
                        this.$router.push("/cgi-bin/koha/erm/agreements")
390
                        setMessage(this.$__("Agreement updated"))
391
                    } else if (response.status == 201) {
392
                        this.$router.push("/cgi-bin/koha/erm/agreements")
393
                        setMessage(this.$__("Agreement created"))
394
                    } else {
395
                        setError(response.message || response.statusText)
396
                    }
382
                    }
397
                })
383
                    this.$router.push("/cgi-bin/koha/erm/agreements")
398
                .catch(error => {
384
                } catch (err) {
399
                    setError(error)
385
                    setError(err.message || err.statusText)
400
                })
386
                }
387
            })();
388
401
        },
389
        },
402
        onStatusChanged(e) {
390
        onStatusChanged(e) {
403
            if (e.authorised_value != "closed") {
391
            if (e.authorised_value != "closed") {
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormConfirmDelete.vue (-24 / +16 lines)
Lines 36-42 Link Here
36
</template>
36
</template>
37
37
38
<script>
38
<script>
39
import { fetchAgreement } from "../../fetch/erm.js"
39
import { ERMAPIClient } from "../../fetch/erm-api-client.js";
40
import { setMessage, setError } from "../../messages"
40
import { setMessage, setError } from "../../messages"
41
41
42
export default {
42
export default {
Lines 53-86 export default { Link Here
53
    },
53
    },
54
    methods: {
54
    methods: {
55
        async getAgreement(agreement_id) {
55
        async getAgreement(agreement_id) {
56
            const agreement = await fetchAgreement(agreement_id)
56
            const client = new ERMAPIClient();
57
            this.agreement = agreement
57
            try {
58
            this.initialized = true
58
                await client.agreements.get(agreement_id).then(data => { this.agreement = data; this.initialized = true; } );
59
            } catch (err) {
60
                setError(err.message || err.statusText)
61
            }
59
        },
62
        },
60
        onSubmit(e) {
63
        onSubmit(e) {
61
            e.preventDefault()
64
            e.preventDefault()
62
65
63
            let apiUrl = "/api/v1/erm/agreements/" + this.agreement.agreement_id
66
            const client = new ERMAPIClient();
64
67
            (async () => {
65
            const options = {
68
                try {
66
                method: "DELETE",
69
                    await client.agreements.delete(this.agreement.agreement_id).then(setMessage(this.$__("Agreement deleted")));
67
                headers: {
70
                    this.$router.push("/cgi-bin/koha/erm/agreements")
68
                    "Content-Type": "application/json;charset=utf-8",
71
                } catch (err) {
69
                },
72
                    setError(err)
70
            }
73
                }
74
            })();
71
75
72
            fetch(apiUrl, options)
73
                .then(response => {
74
                    if (response.status == 204) {
75
                        setMessage(this.$__("Agreement deleted"))
76
                        this.$router.push("/cgi-bin/koha/erm/agreements")
77
                    } else {
78
                        setError(response.message || response.statusText)
79
                    }
80
                })
81
                .catch(error => {
82
                    setError(error)
83
                })
84
        },
76
        },
85
    },
77
    },
86
    name: "AgreementsFormConfirmDelete",
78
    name: "AgreementsFormConfirmDelete",
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue (-2 / +3 lines)
Lines 37-44 Link Here
37
import flatPickr from "vue-flatpickr-component"
37
import flatPickr from "vue-flatpickr-component"
38
import Toolbar from "./AgreementsToolbar.vue"
38
import Toolbar from "./AgreementsToolbar.vue"
39
import { inject, createVNode, render } from "vue"
39
import { inject, createVNode, render } from "vue"
40
import { ERMAPIClient } from "../../fetch/erm-api-client.js";
40
import { storeToRefs } from "pinia"
41
import { storeToRefs } from "pinia"
41
import { fetchAgreements } from "../../fetch/erm.js"
42
import { useDataTable, build_url } from "../../composables/datatables"
42
import { useDataTable, build_url } from "../../composables/datatables"
43
43
44
export default {
44
export default {
Lines 93-99 export default { Link Here
93
    },
93
    },
94
    methods: {
94
    methods: {
95
        async getAgreements() {
95
        async getAgreements() {
96
            const agreements = await fetchAgreements()
96
            const client = new ERMAPIClient()
97
            const agreements = await client.agreements.getAll()
97
            this.agreements = agreements
98
            this.agreements = agreements
98
            this.initialized = true
99
            this.initialized = true
99
        },
100
        },
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsShow.vue (-4 / +8 lines)
Lines 300-306 Link Here
300
300
301
<script>
301
<script>
302
import { inject } from "vue"
302
import { inject } from "vue"
303
import { fetchAgreement } from "../../fetch/erm.js"
303
import { ERMAPIClient } from "../../fetch/erm-api-client.js";
304
import { setError } from "../../messages"
304
305
305
export default {
306
export default {
306
    setup() {
307
    setup() {
Lines 346-354 export default { Link Here
346
    },
347
    },
347
    methods: {
348
    methods: {
348
        async getAgreement(agreement_id) {
349
        async getAgreement(agreement_id) {
349
            const agreement = await fetchAgreement(agreement_id)
350
            const client = new ERMAPIClient();
350
            this.agreement = agreement
351
            try {
351
            this.initialized = true
352
                await client.agreements.get(agreement_id).then(data => { this.agreement = data; this.initialized = true; } );
353
            } catch (err) {
354
                setError(err.message || err.statusText)
355
            }
352
        },
356
        },
353
    },
357
    },
354
    name: "AgreementsShow",
358
    name: "AgreementsShow",
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/EHoldingsLocalPackageAgreements.vue (-2 / +3 lines)
Lines 55-61 Link Here
55
</template>
55
</template>
56
56
57
<script>
57
<script>
58
import { fetchAgreements } from "../../fetch/erm.js"
58
import { ERMAPIClient } from "../../fetch/erm-api-client.js";
59
59
60
export default {
60
export default {
61
    data() {
61
    data() {
Lines 65-71 export default { Link Here
65
        }
65
        }
66
    },
66
    },
67
    beforeCreate() {
67
    beforeCreate() {
68
        fetchAgreements().then(agreements => {
68
        const client = new ERMAPIClient()
69
        const agreements = client.agreements.getAll().then(agreements => {
69
            this.agreements = agreements
70
            this.agreements = agreements
70
            this.initialized = true
71
            this.initialized = true
71
        })
72
        })
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/erm-api-client.js (+52 lines)
Line 0 Link Here
1
import HttpClient from "./http-client";
2
3
export class ERMAPIClient extends HttpClient {
4
  constructor()
5
  {
6
    super({
7
      baseURL: "/api/v1/erm/"
8
    });
9
  }
10
11
  get agreements() {
12
    return {
13
      get: (id) => this.get(
14
        {
15
          endpoint: "agreements/"+id,
16
          headers: {
17
            "x-koha-embed":
18
                "periods,user_roles,user_roles.patron,agreement_licenses,agreement_licenses.license,agreement_relationships,agreement_relationships.related_agreement,documents,agreement_packages,agreement_packages.package,vendor",
19
          }
20
        }
21
      ),
22
      getAll: (query) => this.get(
23
        {
24
          endpoint: "agreements?" + ( query || "_per_page=-1" )
25
        }
26
      ),
27
      delete: (id) => this.delete(
28
        {
29
          endpoint: "agreements/"+id,
30
          headers: this.getDefaultJSONPayloadHeader()
31
        }
32
      ),
33
      create: (agreement) => this.post(
34
        {
35
          endpoint: "agreements",
36
          body: agreement,
37
          headers: this.getDefaultJSONPayloadHeader()
38
        }
39
      ),
40
      update: (agreement, id) => this.put(
41
        {
42
          endpoint: "agreements/" + id,
43
          body: agreement,
44
          headers: this.getDefaultJSONPayloadHeader()
45
        }
46
      )
47
      //count: () => this.count("agreements"), //TODO: Implement count method
48
    };
49
  }
50
}
51
52
export default ERMAPIClient;
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/erm.js (-37 / +1 lines)
Lines 1-42 Link Here
1
import { setError } from "../messages";
1
import { setError } from "../messages";
2
2
3
export const fetchAgreement = async function (agreement_id) {
3
//TODO: all of these functions should be deleted and reimplemented in the components using ERMAPIClient
4
    if (!agreement_id) return;
5
    const apiUrl = "/api/v1/erm/agreements/" + agreement_id;
6
    let agreement;
7
    await fetch(apiUrl, {
8
        headers: {
9
            "x-koha-embed":
10
                "periods,user_roles,user_roles.patron,agreement_licenses,agreement_licenses.license,agreement_relationships,agreement_relationships.related_agreement,documents,agreement_packages,agreement_packages.package,vendor",
11
        },
12
    })
13
        .then(checkError)
14
        .then(
15
            (result) => {
16
                agreement = result;
17
            },
18
            (error) => {
19
                setError(error);
20
            }
21
        );
22
    return agreement;
23
};
24
25
export const fetchAgreements = async function () {
26
    const apiUrl = "/api/v1/erm/agreements?_per_page=-1";
27
    let agreements;
28
    await fetch(apiUrl)
29
        .then(checkError)
30
        .then(
31
            (result) => {
32
                agreements = result;
33
            },
34
            (error) => {
35
                setError(error);
36
            }
37
        );
38
    return agreements;
39
};
40
4
41
export const fetchLicense = async function (license_id) {
5
export const fetchLicense = async function (license_id) {
42
    if (!license_id) return;
6
    if (!license_id) return;
(-)a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/http-client.js (-1 / +60 lines)
Line 0 Link Here
0
- 
1
class HttpClient {
2
    constructor(options = {}) {
3
      this._baseURL = options.baseURL || "";
4
    }
5
6
    async _fetchJSON(endpoint, headers = {}, options = {}) {
7
      const res = await fetch(this._baseURL + endpoint, {
8
        ...options,
9
        headers: headers
10
      })
11
12
      if (!res.ok) throw new Error(res.statusText);
13
14
      if (options.parseResponse !== false && res.status !== 204)
15
        return res.json();
16
17
      return undefined;
18
    }
19
20
    get(params = {}) {
21
      console.log(params);
22
      return this._fetchJSON(params.endpoint, params.headers,
23
      {
24
        ...params.options,
25
        method: "GET"
26
      });
27
    }
28
29
    post(params = {}) {
30
      return this._fetchJSON(params.endpoint, params.headers, {
31
        ...params.options,
32
        body: params.body ? JSON.stringify(params.body) : undefined,
33
        method: "POST"
34
      });
35
    }
36
37
    put(params = {}) {
38
      return this._fetchJSON(params.endpoint, params.headers, {
39
        ...params.options,
40
        body: params.body ? JSON.stringify(params.body) : undefined,
41
        method: "PUT"
42
      });
43
    }
44
45
    delete(params = {}) {
46
      return this._fetchJSON(params.endpoint, params.headers, {
47
        parseResponse: false,
48
        ...params.options,
49
        method: "DELETE"
50
      });
51
    }
52
53
    //TODO: Implement count method
54
55
    getDefaultJSONPayloadHeader(){
56
      return { "Content-Type": "application/json;charset=utf-8" }
57
    }
58
  }
59
60
  export default HttpClient;

Return to bug 32939