@communecter/cocolight-api-client 1.0.28 → 1.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cocolight-api-client.browser.js +3 -3
- package/dist/cocolight-api-client.cjs +1 -1
- package/dist/cocolight-api-client.mjs.js +1 -1
- package/dist/cocolight-api-client.vite.mjs.js +1 -1
- package/dist/cocolight-api-client.vite.mjs.js.map +1 -1
- package/package.json +1 -1
- package/src/Api.js +1 -1
- package/src/api/BaseEntity.js +324 -145
- package/src/api/EndpointApi.js +15 -0
- package/src/api/EndpointApi.types.d.ts +599 -74
- package/src/api/Organization.js +53 -31
- package/src/api/Project.js +27 -32
- package/src/api/User.js +43 -59
- package/src/endpoints.module.js +2 -2
package/package.json
CHANGED
package/src/Api.js
CHANGED
|
@@ -163,7 +163,7 @@ export default class Api {
|
|
|
163
163
|
*/
|
|
164
164
|
async project(projectData) {
|
|
165
165
|
try {
|
|
166
|
-
const project = new Project(this._client, projectData, { EndpointApi, User, Event, Poi, Badge, News });
|
|
166
|
+
const project = new Project(this._client, projectData, { EndpointApi, User, Organization, Event, Poi, Badge, News });
|
|
167
167
|
if (!projectData.id && !projectData.slug) {
|
|
168
168
|
throw new Error("Vous devez fournir un id ou un slug pour créer une instance Project.");
|
|
169
169
|
}
|
package/src/api/BaseEntity.js
CHANGED
|
@@ -54,10 +54,10 @@ export class BaseEntity {
|
|
|
54
54
|
* @throws {ApiError} Si les dépendances ou parent sont invalides.
|
|
55
55
|
*/
|
|
56
56
|
constructor(parent, data = {}, deps = {}, config = {}) {
|
|
57
|
-
this.__entityTag = config.entityTag || this.constructor.name
|
|
57
|
+
this.__entityTag = config.entityTag || this.getEntityTag(this.constructor.name) || "BaseEntity";
|
|
58
58
|
this.deps = deps;
|
|
59
59
|
|
|
60
|
-
if (parent?.__entityTag === "ApiClient") {
|
|
60
|
+
if (this.getEntityTag(parent?.__entityTag) === "ApiClient") {
|
|
61
61
|
/** @type {ApiClient} */
|
|
62
62
|
this.apiClient = parent;
|
|
63
63
|
this.parent = null;
|
|
@@ -66,7 +66,7 @@ export class BaseEntity {
|
|
|
66
66
|
/** @type {ApiClient} */
|
|
67
67
|
this.apiClient = parent.apiClient;
|
|
68
68
|
this.parent = parent;
|
|
69
|
-
if (parent?.__entityTag === "User") {
|
|
69
|
+
if (this.getEntityTag(parent?.__entityTag) === "User") {
|
|
70
70
|
this.userContext = parent;
|
|
71
71
|
} else if (parent?.userContext) {
|
|
72
72
|
this.userContext = parent.userContext;
|
|
@@ -102,6 +102,8 @@ export class BaseEntity {
|
|
|
102
102
|
this.data = proxy;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
+
getEntityTag = (__entityTag) => __entityTag?.replace(/^_/, "");
|
|
106
|
+
|
|
105
107
|
/** @returns {string|null} Identifiant de l'entité */
|
|
106
108
|
get id() {
|
|
107
109
|
return this._draftData.id || null;
|
|
@@ -1000,7 +1002,14 @@ export class BaseEntity {
|
|
|
1000
1002
|
* @private
|
|
1001
1003
|
*/
|
|
1002
1004
|
_linkEntities(results) {
|
|
1003
|
-
return results.
|
|
1005
|
+
return results.flatMap(d => {
|
|
1006
|
+
if (!d?.collection) {
|
|
1007
|
+
this.apiClient._logger?.warn?.(`Objet ignoré car sans 'collection' : ${d?.id}`);
|
|
1008
|
+
return []; // exclu de la liste
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
return [this._linkEntity?.(d.collection, d) ?? d];
|
|
1012
|
+
});
|
|
1004
1013
|
}
|
|
1005
1014
|
|
|
1006
1015
|
/**
|
|
@@ -1668,193 +1677,152 @@ export class BaseEntity {
|
|
|
1668
1677
|
* Récupérer les organisations d'une entitée : la liste des organisations dont l'entité est membre ou admin valide.
|
|
1669
1678
|
* Constant : GET_ORGANIZATIONS_ADMIN | GET_ORGANIZATIONS_NO_ADMIN
|
|
1670
1679
|
* @param {Object} data - Les données à envoyer.
|
|
1680
|
+
* @param {boolean} isNext - Indique si c'est une recherche suivante (pagination).
|
|
1671
1681
|
* @returns {Promise<Object>} - Les données de réponse.
|
|
1672
1682
|
*/
|
|
1673
|
-
async getOrganizations(data = {}) {
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
? () => this.callIsMe(() => this.endpointApi.getOrganizationsAdmin(data))
|
|
1694
|
-
: () => this.endpointApi.getOrganizationsNoAdmin(data);
|
|
1695
|
-
|
|
1696
|
-
const arrayObjet = await fetchFn();
|
|
1697
|
-
|
|
1698
|
-
if (!Array.isArray(arrayObjet.results)) {
|
|
1699
|
-
throw new ApiResponseError("Erreur lors de la récupération des organisations.", 500, arrayObjet.results);
|
|
1700
|
-
}
|
|
1701
|
-
|
|
1702
|
-
// nettoyage du count
|
|
1703
|
-
delete arrayObjet?.count?.spam;
|
|
1704
|
-
|
|
1705
|
-
// calcul du total
|
|
1706
|
-
const totalCount = Object.values(arrayObjet.count || {}).reduce((acc, val) => acc + val, 0);
|
|
1707
|
-
arrayObjet.count.total = totalCount;
|
|
1708
|
-
|
|
1709
|
-
const rawList = this._linkEntities(arrayObjet.results);
|
|
1683
|
+
async getOrganizations(data = {}, isNext = false) {
|
|
1684
|
+
data.searchType = this._getDefaultFromEndpoint("GET_ORGANIZATIONS_NO_ADMIN", "searchType");
|
|
1685
|
+
// data.searchBy = "ALL";
|
|
1686
|
+
return this._paginateWith(data, isNext, async (finalData) => {
|
|
1687
|
+
if(this.isMe){
|
|
1688
|
+
finalData.pathParams = { type: this.getEntityType(), id: this.id };
|
|
1689
|
+
// NOTE : dans le schema je crois que si pas de finalData.filters alors le default ce fait avec finalData.pathParams
|
|
1690
|
+
// finalData.filters = {
|
|
1691
|
+
// [`links.members.${this.id}`]: { "$exists": true },
|
|
1692
|
+
// [`links.members.${this.id}.toBeValidated`]: { "$exists": false },
|
|
1693
|
+
// [`links.members.${this.id}.isInviting`]: { "$exists": false }
|
|
1694
|
+
// };
|
|
1695
|
+
} else {
|
|
1696
|
+
delete finalData?.pathParams;
|
|
1697
|
+
finalData.filters = {
|
|
1698
|
+
[`links.members.${this.id}`]: { "$exists": true },
|
|
1699
|
+
[`links.members.${this.id}.toBeValidated`]: { "$exists": false },
|
|
1700
|
+
[`links.members.${this.id}.isInviting`]: { "$exists": false }
|
|
1701
|
+
};
|
|
1702
|
+
}
|
|
1710
1703
|
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1704
|
+
const fetchFn = this.isMe
|
|
1705
|
+
? () => this.callIsMe(() => this.endpointApi.getOrganizationsAdmin(finalData))
|
|
1706
|
+
: () => this.endpointApi.getOrganizationsNoAdmin(finalData);
|
|
1707
|
+
|
|
1708
|
+
return fetchFn();
|
|
1709
|
+
});
|
|
1715
1710
|
}
|
|
1716
1711
|
|
|
1717
1712
|
/**
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1713
|
+
* Récupérer les projets d'une entitée : liste des projets de l'entité ou elle est "parent" ou "contributeur".
|
|
1714
|
+
* Constant : GET_PROJECTS_ADMIN | GET_PROJECTS_NO_ADMIN
|
|
1715
|
+
* @param {Object} data - Les données à envoyer.
|
|
1716
|
+
* @param {boolean} isNext - Indique si c'est une recherche suivante (pagination).
|
|
1717
|
+
* @returns {Promise<Object>} - Les données de réponse.
|
|
1718
|
+
*/
|
|
1719
|
+
async getProjects(data = {}, isNext = false) {
|
|
1720
|
+
data.searchType = this._getDefaultFromEndpoint("GET_PROJECTS_ADMIN", "searchType");
|
|
1721
|
+
// data.searchBy = "ALL";
|
|
1722
|
+
return this._paginateWith(data, isNext, async (finalData) => {
|
|
1723
|
+
if(this.isMe){
|
|
1724
|
+
finalData.pathParams = { type: this.getEntityType(), id: this.id };
|
|
1725
|
+
// NOTE : dans le schema je crois que si pas de finalData.filters alors le default ce fait avec finalData.pathParams
|
|
1726
|
+
// finalData.filters = {
|
|
1729
1727
|
// "$or": {
|
|
1730
1728
|
// [`links.contributors.${this.id}`]: { "$exists": true },
|
|
1731
1729
|
// [`parent.${this.id}`]: { "$exists": true }
|
|
1732
1730
|
// },
|
|
1733
1731
|
// [`links.contributors.${this.id}`]: { "$exists": true }
|
|
1734
1732
|
// };
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
const fetchFn = this.isMe
|
|
1747
|
-
? () => this.callIsMe(() => this.endpointApi.getProjectsAdmin(data))
|
|
1748
|
-
: () => this.endpointApi.getProjectsNoAdmin(data);
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
const arrayObjet = await fetchFn();
|
|
1733
|
+
} else {
|
|
1734
|
+
delete finalData?.pathParams;
|
|
1735
|
+
finalData.filters = {
|
|
1736
|
+
"$or": {
|
|
1737
|
+
[`links.contributors.${this.id}`]: { "$exists": true },
|
|
1738
|
+
[`parent.${this.id}`]: { "$exists": true }
|
|
1739
|
+
},
|
|
1740
|
+
[`links.contributors.${this.id}`]: { "$exists": true }
|
|
1741
|
+
};
|
|
1742
|
+
}
|
|
1752
1743
|
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1744
|
+
const fetchFn = this.isMe
|
|
1745
|
+
? () => this.callIsMe(() => this.endpointApi.getProjectsAdmin(finalData))
|
|
1746
|
+
: () => this.endpointApi.getProjectsNoAdmin(finalData);
|
|
1756
1747
|
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
return {
|
|
1760
|
-
count: arrayObjet?.count?.projects ?? 0,
|
|
1761
|
-
results: rawList
|
|
1762
|
-
};
|
|
1748
|
+
return fetchFn();
|
|
1749
|
+
});
|
|
1763
1750
|
}
|
|
1764
1751
|
|
|
1765
1752
|
/**
|
|
1766
1753
|
* Récupérer les POIs d'une entité : liste des POIs de l'entité ou elle est "parent".
|
|
1767
1754
|
* Constant : GET_POIS_NO_ADMIN / GET_POIS_ADMIN
|
|
1768
1755
|
* @param {Object} data - Les données à envoyer.
|
|
1756
|
+
* @param {boolean} isNext - Indique si c'est une recherche suivante (pagination).
|
|
1769
1757
|
* @returns {Promise<Object>} - Les données de réponse.
|
|
1770
1758
|
*/
|
|
1771
|
-
async getPois(data = {}) {
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1759
|
+
async getPois(data = {}, isNext = false) {
|
|
1760
|
+
data.searchType = this._getDefaultFromEndpoint("GET_POIS_ADMIN", "searchType");
|
|
1761
|
+
// data.searchBy = "ALL";
|
|
1762
|
+
return this._paginateWith(data, isNext, async (finalData) => {
|
|
1763
|
+
if(this.isMe){
|
|
1764
|
+
finalData.pathParams = { type: this.getEntityType(), id: this.id };
|
|
1765
|
+
// NOTE : dans le schema je crois que si pas de finalData.filters alors le default ce fait avec finalData.pathParams
|
|
1766
|
+
// finalData.filters = {
|
|
1777
1767
|
// [`parent.${this.id}`]: { "$exists": true },
|
|
1778
1768
|
// };
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
const fetchFn = this.isMe
|
|
1787
|
-
? () => this.callIsMe(() => this.endpointApi.getPoisAdmin(data))
|
|
1788
|
-
: () => this.endpointApi.getPoisNoAdmin(data);
|
|
1769
|
+
} else {
|
|
1770
|
+
delete finalData?.pathParams;
|
|
1771
|
+
finalData.filters = {
|
|
1772
|
+
[`parent.${this.id}`]: { "$exists": true },
|
|
1773
|
+
};
|
|
1774
|
+
}
|
|
1789
1775
|
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
}
|
|
1776
|
+
const fetchFn = this.isMe
|
|
1777
|
+
? () => this.callIsMe(() => this.endpointApi.getPoisAdmin(finalData))
|
|
1778
|
+
: () => this.endpointApi.getPoisNoAdmin(finalData);
|
|
1794
1779
|
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
return {
|
|
1799
|
-
count: arrayObjet.count?.poi ?? 0,
|
|
1800
|
-
results: rawList
|
|
1801
|
-
};
|
|
1780
|
+
return fetchFn();
|
|
1781
|
+
});
|
|
1802
1782
|
}
|
|
1803
1783
|
|
|
1804
1784
|
/**
|
|
1805
1785
|
* Récupérer les abonnés d'une entité
|
|
1806
1786
|
* Constant : GET_SUBSCRIBERS
|
|
1807
1787
|
* @param {Object} data - Les données à envoyer.
|
|
1788
|
+
* @param {boolean} isNext - Indique si c'est une recherche suivante (pagination).
|
|
1808
1789
|
* @returns {Promise<Object>} - Les données de réponse.
|
|
1809
1790
|
*/
|
|
1810
|
-
async getSubscribers(data = {}) {
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
data
|
|
1814
|
-
|
|
1815
|
-
[`links.follows.${this.id}.toBeValidated`]: { "$exists": false },
|
|
1816
|
-
[`links.follows.${this.id}.isInviting`]: { "$exists": false }
|
|
1817
|
-
};
|
|
1818
|
-
|
|
1819
|
-
const arrayObjet = await this.endpointApi.getSubscribers(data);
|
|
1820
|
-
if (!Array.isArray(arrayObjet.results)) {
|
|
1821
|
-
throw new ApiResponseError("Erreur lors de la récupération des abonnés.", 500, arrayObjet.results);
|
|
1822
|
-
}
|
|
1791
|
+
async getSubscribers(data = {}, isNext = false) {
|
|
1792
|
+
data.searchType = this._getDefaultFromEndpoint("GET_SUBSCRIBERS", "searchType");
|
|
1793
|
+
// data.searchBy = "ALL";
|
|
1794
|
+
return this._paginateWith(data, isNext, async (finalData) => {
|
|
1795
|
+
delete finalData?.pathParams;
|
|
1823
1796
|
|
|
1824
|
-
|
|
1825
|
-
|
|
1797
|
+
finalData.filters = {
|
|
1798
|
+
[`links.follows.${this.id}`]: { "$exists": true },
|
|
1799
|
+
[`links.follows.${this.id}.toBeValidated`]: { "$exists": false },
|
|
1800
|
+
[`links.follows.${this.id}.isInviting`]: { "$exists": false }
|
|
1801
|
+
};
|
|
1826
1802
|
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
results: rawList
|
|
1830
|
-
};
|
|
1803
|
+
return this.endpointApi.getSubscribers(finalData);
|
|
1804
|
+
});
|
|
1831
1805
|
}
|
|
1832
1806
|
|
|
1833
1807
|
/**
|
|
1834
1808
|
* Liste des badges créés par l'entité
|
|
1835
1809
|
* Constant : GET_BADGES
|
|
1836
1810
|
* @param {Object} data - Les données à envoyer.
|
|
1811
|
+
* @param {boolean} isNext - Indique si c'est une recherche suivante (pagination).
|
|
1837
1812
|
* @returns {Promise<Object>} - Les données de réponse.
|
|
1838
1813
|
*/
|
|
1839
|
-
async getBadgesIssuer(data = {}) {
|
|
1840
|
-
|
|
1814
|
+
async getBadgesIssuer(data = {}, isNext = false) {
|
|
1815
|
+
data.searchType = this._getDefaultFromEndpoint("GET_BADGES", "searchType");
|
|
1816
|
+
// data.searchBy = "ALL";
|
|
1817
|
+
return this._paginateWith(data, isNext, async (finalData) => {
|
|
1818
|
+
delete finalData?.pathParams;
|
|
1841
1819
|
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
const arrayObjet = await this.endpointApi.getBadges(data);
|
|
1847
|
-
if (!Array.isArray(arrayObjet.results)) {
|
|
1848
|
-
throw new ApiResponseError("Erreur lors de la récupération des badges.", 500, arrayObjet.results);
|
|
1849
|
-
}
|
|
1820
|
+
finalData.filters = finalData.filters || {};
|
|
1821
|
+
finalData.filters["$or"] = {};
|
|
1822
|
+
finalData.filters["$or"][`issuer.${this.id}`] = { "$exists": true };
|
|
1850
1823
|
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
return {
|
|
1855
|
-
count: arrayObjet.count?.badges ?? 0,
|
|
1856
|
-
results: rawList
|
|
1857
|
-
};
|
|
1824
|
+
return this.endpointApi.getBadges(finalData);
|
|
1825
|
+
});
|
|
1858
1826
|
}
|
|
1859
1827
|
|
|
1860
1828
|
/**
|
|
@@ -2162,6 +2130,217 @@ export class BaseEntity {
|
|
|
2162
2130
|
return this._isLinked("follows");
|
|
2163
2131
|
}
|
|
2164
2132
|
|
|
2133
|
+
/**
|
|
2134
|
+
* Récupère le JSON personnalisé de l'entité
|
|
2135
|
+
*
|
|
2136
|
+
* @returns {Promise<Object>} - Le JSON personnalisé de l'entité.
|
|
2137
|
+
* @throws {ApiError} - Si le slug de l'entité n'est pas défini.
|
|
2138
|
+
*/
|
|
2139
|
+
async getCostumJson() {
|
|
2140
|
+
if (!this.serverData.slug) {
|
|
2141
|
+
throw new ApiError("slug de l'entité non défini");
|
|
2142
|
+
}
|
|
2143
|
+
const data = {
|
|
2144
|
+
pathParams: {
|
|
2145
|
+
slug: this.serverData.slug
|
|
2146
|
+
}
|
|
2147
|
+
};
|
|
2148
|
+
return this.endpointApi.getCostumJson(data);
|
|
2149
|
+
}
|
|
2150
|
+
|
|
2151
|
+
/**
|
|
2152
|
+
* Génère des plages d'index pour la pagination.
|
|
2153
|
+
*
|
|
2154
|
+
* @param {Array<string>} searchType - Types de recherche.
|
|
2155
|
+
* @param {number} indexStep - Pas d'index.
|
|
2156
|
+
* @param {Object} previousRanges - Plages précédentes.
|
|
2157
|
+
* @returns {Object} - Plages d'index générées.
|
|
2158
|
+
* @private
|
|
2159
|
+
*/
|
|
2160
|
+
_generateRanges(searchType, indexStep, previousRanges = {}) {
|
|
2161
|
+
const ranges = {};
|
|
2162
|
+
for (const type of searchType) {
|
|
2163
|
+
const previous = previousRanges[type] || { indexMax: 0 };
|
|
2164
|
+
ranges[type] = {
|
|
2165
|
+
indexMin: previous.indexMax || 0,
|
|
2166
|
+
indexMax: previous.indexMax + indexStep
|
|
2167
|
+
};
|
|
2168
|
+
}
|
|
2169
|
+
return ranges;
|
|
2170
|
+
}
|
|
2171
|
+
|
|
2172
|
+
/**
|
|
2173
|
+
* Normalise le compte des résultats.
|
|
2174
|
+
*
|
|
2175
|
+
* @param {Object} count - Compte des résultats.
|
|
2176
|
+
* @returns {Object} - Compte normalisé.
|
|
2177
|
+
* @private
|
|
2178
|
+
*/
|
|
2179
|
+
_normalizeCount(count = {}) {
|
|
2180
|
+
// suppression des indésirables
|
|
2181
|
+
delete count.spam;
|
|
2182
|
+
|
|
2183
|
+
// calcul du total (somme des valeurs numériques)
|
|
2184
|
+
count.total = Object.values(count).reduce((acc, val) => acc + (typeof val === "number" ? val : 0), 0);
|
|
2185
|
+
|
|
2186
|
+
return count;
|
|
2187
|
+
}
|
|
2188
|
+
|
|
2189
|
+
/**
|
|
2190
|
+
* Méthode générique de pagination contextuelle avec support next / prev.
|
|
2191
|
+
*
|
|
2192
|
+
* @param {Object} data - Données d'entrée
|
|
2193
|
+
* @param {boolean} isNext - true si appel de page suivante
|
|
2194
|
+
* @param {Function} finalizer - fonction qui transforme et envoie les données (exécute la requête)
|
|
2195
|
+
* @returns {Promise<Object>} { count, results, next, prev }
|
|
2196
|
+
* @throws {ApiError} - Si le slug ou l'id de l'entité n'est pas défini.
|
|
2197
|
+
* @throws {ApiResponseError} - Si les résultats ne sont pas un tableau.
|
|
2198
|
+
* @private
|
|
2199
|
+
*/
|
|
2200
|
+
async _paginateWith(data = {}, isNext = false, finalizer) {
|
|
2201
|
+
if (!this.serverData.slug) throw new ApiError("slug de l'entité non défini");
|
|
2202
|
+
if (!this.serverData.id) throw new ApiError("id de l'entité non défini");
|
|
2203
|
+
|
|
2204
|
+
const hasStep = (d) => d?.indexStep && d.indexStep > 0;
|
|
2205
|
+
|
|
2206
|
+
if (!this._paginationCursor || (!isNext && this._paginationHistory.length === 0)) {
|
|
2207
|
+
this._paginationCount = 0;
|
|
2208
|
+
this._paginationPageIndex = 0;
|
|
2209
|
+
this._paginationHistory = [];
|
|
2210
|
+
this._paginationPageSizes = [];
|
|
2211
|
+
data.countType = data.searchType;
|
|
2212
|
+
|
|
2213
|
+
if (!data?.searchBy && hasStep(data)) {
|
|
2214
|
+
data.ranges = this._generateRanges(data.searchType, data.indexStep);
|
|
2215
|
+
data.indexMin = data.indexMin ?? 0;
|
|
2216
|
+
data.indexMax = data.indexMax ?? data.indexStep;
|
|
2217
|
+
} else if (data?.searchBy === "ALL" && hasStep(data)) {
|
|
2218
|
+
data.indexMin = data.indexMin ?? 0;
|
|
2219
|
+
data.indexMax = data.indexMax ?? data.indexStep;
|
|
2220
|
+
}
|
|
2221
|
+
|
|
2222
|
+
this._paginationCursor = { ...data };
|
|
2223
|
+
}
|
|
2224
|
+
|
|
2225
|
+
const cursor = this._paginationCursor;
|
|
2226
|
+
|
|
2227
|
+
if (isNext) {
|
|
2228
|
+
this._paginationHistory.push({ ...cursor });
|
|
2229
|
+
|
|
2230
|
+
if (!cursor.searchBy && hasStep(cursor)) {
|
|
2231
|
+
cursor.ranges = this._generateRanges(cursor.searchType, cursor.indexStep, cursor.ranges);
|
|
2232
|
+
cursor.indexMin = cursor.indexMax ?? 0;
|
|
2233
|
+
cursor.indexMax = (cursor.indexMax ?? 0) + cursor.indexStep;
|
|
2234
|
+
} else if (cursor.searchBy === "ALL" && hasStep(cursor)) {
|
|
2235
|
+
cursor.indexMin = cursor.indexMax ?? 0;
|
|
2236
|
+
cursor.indexMax = (cursor.indexMax ?? 0) + cursor.indexStep;
|
|
2237
|
+
}
|
|
2238
|
+
|
|
2239
|
+
this._paginationCursor = { ...cursor };
|
|
2240
|
+
}
|
|
2241
|
+
|
|
2242
|
+
data = { ...this._paginationCursor };
|
|
2243
|
+
|
|
2244
|
+
if (!isNext && (!data?.searchType || !Array.isArray(data.searchType) || data.searchType.length === 0)) {
|
|
2245
|
+
throw new ApiError("searchType non défini");
|
|
2246
|
+
}
|
|
2247
|
+
|
|
2248
|
+
const result = await finalizer(data);
|
|
2249
|
+
if (!Array.isArray(result.results)) {
|
|
2250
|
+
throw new ApiResponseError("Erreur lors de la récupération des résultats", 500, result.results);
|
|
2251
|
+
}
|
|
2252
|
+
|
|
2253
|
+
this._paginationCount += result.results.length;
|
|
2254
|
+
this._paginationPageSizes.push(result.results.length);
|
|
2255
|
+
|
|
2256
|
+
if (isNext) {
|
|
2257
|
+
this._paginationPageIndex++;
|
|
2258
|
+
}
|
|
2259
|
+
|
|
2260
|
+
const count = this._normalizeCount(result.count);
|
|
2261
|
+
const rawList = this._linkEntities(result.results);
|
|
2262
|
+
const hasNext = hasStep(data) && this._paginationCount < count.total;
|
|
2263
|
+
const hasPrev = this._paginationHistory?.length > 0;
|
|
2264
|
+
|
|
2265
|
+
const response = {
|
|
2266
|
+
count,
|
|
2267
|
+
results: rawList,
|
|
2268
|
+
pageIndex: this._paginationPageIndex,
|
|
2269
|
+
pageNumber: this._paginationPageIndex + 1,
|
|
2270
|
+
hasNext,
|
|
2271
|
+
hasPrev
|
|
2272
|
+
};
|
|
2273
|
+
|
|
2274
|
+
if (hasNext) {
|
|
2275
|
+
response.next = async () => this._paginateWith({}, true, finalizer);
|
|
2276
|
+
}
|
|
2277
|
+
|
|
2278
|
+
if (this._paginationHistory?.length > 0) {
|
|
2279
|
+
response.prev = async () => {
|
|
2280
|
+
const previous = this._paginationHistory.pop();
|
|
2281
|
+
const lastPageSize = this._paginationPageSizes.pop() ?? 0;
|
|
2282
|
+
this._paginationCount -= lastPageSize;
|
|
2283
|
+
this._paginationPageIndex = Math.max(0, this._paginationPageIndex - 1);
|
|
2284
|
+
this._paginationCursor = { ...previous };
|
|
2285
|
+
return this._paginateWith({}, false, finalizer);
|
|
2286
|
+
};
|
|
2287
|
+
}
|
|
2288
|
+
|
|
2289
|
+
return response;
|
|
2290
|
+
}
|
|
2291
|
+
|
|
2292
|
+
|
|
2293
|
+
/**
|
|
2294
|
+
* Réinitialise l'état de pagination.
|
|
2295
|
+
*/
|
|
2296
|
+
resetPagination() {
|
|
2297
|
+
this._paginationCursor = undefined;
|
|
2298
|
+
this._paginationCount = 0;
|
|
2299
|
+
this._paginationPageIndex = 0;
|
|
2300
|
+
this._paginationHistory = [];
|
|
2301
|
+
this._paginationPageSizes = [];
|
|
2302
|
+
}
|
|
2303
|
+
|
|
2304
|
+
|
|
2305
|
+
/**
|
|
2306
|
+
* Récupère une valeur par défaut depuis un endpoint donné.
|
|
2307
|
+
*
|
|
2308
|
+
* @param {string} constant - Le nom unique de l’endpoint (ex: "GET_ORGANIZATIONS_NO_ADMIN")
|
|
2309
|
+
* @param {string} path - Le chemin vers la propriété (ex: "searchType")
|
|
2310
|
+
* @returns {*} La valeur par défaut, ou undefined si non trouvée
|
|
2311
|
+
*/
|
|
2312
|
+
_getDefaultFromEndpoint(constant, path) {
|
|
2313
|
+
const endpoint = this.apiClient._endpoints.find((ep) => ep.constant === constant);
|
|
2314
|
+
if (!endpoint?.request?.properties?.[path]) return undefined;
|
|
2315
|
+
return endpoint.request.properties[path].default;
|
|
2316
|
+
}
|
|
2317
|
+
|
|
2318
|
+
/**
|
|
2319
|
+
* recherche lié à l'entité.
|
|
2320
|
+
*
|
|
2321
|
+
* @param {Object} data - Les données de recherche.
|
|
2322
|
+
* @param {boolean} isNext - Indique si c'est une recherche suivante (pagination).
|
|
2323
|
+
* @returns {Promise<Object>} - Résultat de la recherche.
|
|
2324
|
+
* @throws {ApiError} - Si le slug ou l'id de entité n'est pas défini.
|
|
2325
|
+
*/
|
|
2326
|
+
async searchCostum(data = {}, isNext = false) {
|
|
2327
|
+
return this._paginateWith(data, isNext, async (finalData) => {
|
|
2328
|
+
finalData = {
|
|
2329
|
+
...finalData,
|
|
2330
|
+
costumSlug: this.serverData.slug,
|
|
2331
|
+
contextId: this.serverData.id,
|
|
2332
|
+
contextType: this.getEntityType()
|
|
2333
|
+
};
|
|
2334
|
+
|
|
2335
|
+
if (finalData.sourceKey?.length) {
|
|
2336
|
+
finalData.sourceKey = [...finalData.sourceKey, this.serverData.slug];
|
|
2337
|
+
} else {
|
|
2338
|
+
finalData.sourceKey = [this.serverData.slug];
|
|
2339
|
+
}
|
|
2340
|
+
|
|
2341
|
+
return this.endpointApi.globalAutocompleteCostum(finalData);
|
|
2342
|
+
});
|
|
2343
|
+
}
|
|
2165
2344
|
|
|
2166
2345
|
|
|
2167
2346
|
}
|
package/src/api/EndpointApi.js
CHANGED
|
@@ -1363,6 +1363,21 @@ class EndpointApi {
|
|
|
1363
1363
|
return this.call("GET_COSTUM_JSON", data);
|
|
1364
1364
|
}
|
|
1365
1365
|
|
|
1366
|
+
/**
|
|
1367
|
+
* Recherche globale lier à un costum : Effectue une recherche globale lier à un costum
|
|
1368
|
+
* Constant : GLOBAL_AUTOCOMPLETE_COSTUM
|
|
1369
|
+
* @param {import("./EndpointApi.types").GlobalAutocompleteCostumData} data - Données envoyées à l'API
|
|
1370
|
+
* @returns {Promise<Object>} - Les données de réponse.
|
|
1371
|
+
* @throws {ApiResponseError} - En cas d'erreur détectée dans la réponse.
|
|
1372
|
+
* @throws {Error} - En cas d'erreur inattendue.
|
|
1373
|
+
*/
|
|
1374
|
+
async globalAutocompleteCostum(data) {
|
|
1375
|
+
if (!data || typeof data !== "object") {
|
|
1376
|
+
throw new TypeError("Le paramètre data doit être un objet.");
|
|
1377
|
+
}
|
|
1378
|
+
return this.call("GLOBAL_AUTOCOMPLETE_COSTUM", data);
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1366
1381
|
}
|
|
1367
1382
|
|
|
1368
1383
|
export default EndpointApi;
|