Added getSites/SiteAssign endpoints + doc
MODIFY
src/assets/server/rest/v1/docs/e-mobility-oas.json
src/assets/server/rest/v1/docs/e-mobility-oas.json
}
}
},
+ "/api/users/{id}/sites": {
+ "get": {
+ "security": [
+ {
+ "bearerAuth": []
+ }
+ ],
+ "description": "Get user's assigned sites",
+ "parameters": [
+ {
+ "$ref": "#/components/parameters/userID"
+ }
+ ],
+ "tags": [
+ "Users"
+ ],
+ "responses": {
+ "200": {
+ "description": "Retrieved assigned sites list",
+ "content": {
+ "application/json": {
+ "example": {
+ "count": 1,
+ "result": [
+ {
+ "userID": "###",
+ "siteAdmin": false,
+ "site": {
+ "address": {
+ "city": "###",
+ "country": "###"
+ },
+ "name": "###",
+ "id": "###"
+ }
+ }
+ ]
+ }
+ }
+ }
+ },
+ "401": {
+ "$ref": "#/components/responses/UnauthorizedError"
+ },
+ "403": {
+ "$ref": "#/components/responses/ForbiddenError"
+ },
+ "500": {
+ "$ref": "#/components/responses/BackendError"
+ },
+ "550": {
+ "$ref": "#/components/responses/ResourceDoesNotExists"
+ }
+ }
+ },
+ "post": {
+ "security": [
+ {
+ "bearerAuth": []
+ }
+ ],
+ "description": "Assign sites to a user",
+ "tags": [
+ "Users"
+ ],
+ "requestBody": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "properties": {
+ "siteIDs": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "responses": {
+ "200": {
+ "description": "User successfully assigned to sites",
+ "content": {
+ "application/json": {
+ "example": {
+ "status": "Success"
+ }
+ }
+ }
+ },
+ "401": {
+ "$ref": "#/components/responses/UnauthorizedError"
+ },
+ "403": {
+ "$ref": "#/components/responses/ForbiddenError"
+ },
+ "500": {
+ "$ref": "#/components/responses/BackendError"
+ },
+ "550": {
+ "$ref": "#/components/responses/ResourceDoesNotExists"
+ }
+ }
+ }
+ },
"/api/tags": {
"get": {
"security": [

MODIFY
src/server/rest/v1/router/api/UserRouter.ts
src/server/rest/v1/router/api/UserRouter.ts
@@ -19,6 +19,8 @@ export default class UserRouter {
this.buildRouteUpdateUser();
this.buildRouteDeleteUser();
this.buildRouteUserDefaultCarTag();
+ this.buildRouteUserGetSites();
+ this.buildRouteUserSiteAssign();
return this.router;
}
@@ -60,4 +62,18 @@ export default class UserRouter {
await RouterUtils.handleServerAction(UserService.handleGetUserDefaultTagCar.bind(this), ServerAction.USER_DEFAULT_TAG_CAR, req, res, next);
});
}
+
+ protected buildRouteUserGetSites(): void {
+ this.router.get(`/${ServerRoute.REST_USER_SITES}`, async (req: Request, res: Response, next: NextFunction) => {
+ req.query.UserID = req.params.id;
+ await RouterUtils.handleServerAction(UserService.handleGetSites.bind(this), ServerAction.USER_SITES, req, res, next);
+ });
+ }
+
+ protected buildRouteUserSiteAssign(): void {
+ this.router.post(`/${ServerRoute.REST_USER_SITES}`, async (req: Request, res: Response, next: NextFunction) => {
+ req.body.userID = req.params.id;
+ await RouterUtils.handleServerAction(UserService.handleAssignSitesToUser.bind(this), ServerAction.ADD_SITES_TO_USER, req, res, next);
+ });
+ }
}

MODIFY
src/types/Server.ts
src/types/Server.ts
@@ -495,6 +495,7 @@ export enum ServerRoute {
REST_USERS = 'users',
REST_USER = 'users/:id',
REST_USER_DEFAULT_TAG_CAR = 'users/:id/default-car-tag',
+ REST_USER_SITES = 'users/:id/sites',
REST_TAGS = 'tags',
REST_TAG = 'tags/:id',

MODIFY
test/api/SiteOrgTest.ts
test/api/SiteOrgTest.ts
@@ -65,6 +65,16 @@ async function assignUserToSite(userRole, site): Promise<any> {
return testData.userService.siteApi.addUsersToSite(site.id, [userContext.id]);
}
+/**
+ * @param userRole
+ * @param sites
+ */
+async function assignSitesToUser(userRole, sites: string[]): Promise<any> {
+ // Assign the user to the site
+ const userContext = await testData.tenantContext.getUserContext(userRole);
+ return testData.userService.siteApi.addSitesToUser(userContext.id, sites);
+}
+
/**
* @param userRole
* @param site
@@ -172,6 +182,14 @@ describe('Site tests', function() {
expect(res.data.result.map((site) => site.user.id)).to.contain(userContext.id);
});
+ it('Should be able to assign a site to a user', async () => {
+ await assignSitesToUser(ContextDefinition.USER_CONTEXTS.BASIC_USER, [testData.newSite]);
+ const res = await testData.userService.siteApi.readUsersForSite(testData.newSite.id);
+ expect(res.data.count).to.eq(1);
+ const userContext = await testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.BASIC_USER);
+ expect(res.data.result.map((site) => site.user.id)).to.contain(userContext.id);
+ });
+
it('Should be able to assign site admin to a site', async () => {
await assignSiteAdmin(ContextDefinition.USER_CONTEXTS.BASIC_USER, testData.newSite);
const res = await testData.userService.siteApi.readUsersForSite(testData.newSite.id);

MODIFY
test/api/client/SiteApi.ts
test/api/client/SiteApi.ts
import CrudApi from './utils/CrudApi';
+import { ServerRoute } from '../../../src/types/Server';
import TestConstants from './utils/TestConstants';
export default class SiteApi extends CrudApi {
@@ -34,6 +35,13 @@ export default class SiteApi extends CrudApi {
}, '/client/api/AddUsersToSite');
}
+ public async addSitesToUser(userId: string, siteIds: string[]) {
+ const url = this.buildRestEndpointUrl(ServerRoute.REST_USER_SITES, { id: userId });
+ return super.create({
+ siteIDs: siteIds,
+ }, url);
+ }
+
public async readUsersForSite(siteId) {
return super.read({
SiteID: siteId

Added getSites/SiteAssign endpoints + doc