Added unassignSitesToUser endpoint + doc
MODIFY
src/assets/server/rest/v1/docs/e-mobility-oas.json
src/assets/server/rest/v1/docs/e-mobility-oas.json
"$ref": "#/components/responses/ResourceDoesNotExists"
}
}
+ },
+ "delete": {
+ "security": [
+ {
+ "bearerAuth": []
+ }
+ ],
+ "description": "Unassign sites to a user",
+ "tags": [
+ "Users"
+ ],
+ "requestBody": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "properties": {
+ "siteIDs": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "responses": {
+ "200": {
+ "description": "User successfully unassigned 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": {

MODIFY
src/server/rest/v1/router/api/UserRouter.ts
src/server/rest/v1/router/api/UserRouter.ts
@@ -21,6 +21,7 @@ export default class UserRouter {
this.buildRouteUserDefaultCarTag();
this.buildRouteUserGetSites();
this.buildRouteUserSiteAssign();
+ this.buildRouteUserSiteUnassign();
return this.router;
}
@@ -76,4 +77,11 @@ export default class UserRouter {
await RouterUtils.handleServerAction(UserService.handleAssignSitesToUser.bind(this), ServerAction.ADD_SITES_TO_USER, req, res, next);
});
}
+
+ protected buildRouteUserSiteUnassign(): void {
+ this.router.put(`/${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.REMOVE_SITES_FROM_USER, req, res, next);
+ });
+ }
}

MODIFY
test/api/SiteOrgTest.ts
test/api/SiteOrgTest.ts
@@ -75,6 +75,17 @@ async function assignSitesToUser(userRole, sites: string[]): Promise<any> {
return testData.userService.siteApi.addSitesToUser(userContext.id, sites);
}
+
+/**
+ * @param userRole
+ * @param sites
+ */
+async function unassignSitesToUser(userRole, sites: string[]): Promise<any> {
+ // Assign the user to the site
+ const userContext = await testData.tenantContext.getUserContext(userRole);
+ return testData.userService.siteApi.unassignSitesToUser(userContext.id, sites);
+}
+
/**
* @param userRole
* @param site
@@ -178,12 +189,22 @@ describe('Site tests', function() {
await assignUserToSite(ContextDefinition.USER_CONTEXTS.BASIC_USER, testData.newSite);
const res = await testData.userService.siteApi.readUsersForSite(testData.newSite.id);
expect(res.data.count).to.eq(1);
+ console.log(res.data.result);
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 unassign a site to a user', async () => {
+ await unassignSitesToUser(ContextDefinition.USER_CONTEXTS.BASIC_USER, [testData.newSite.id]);
+ const res = await testData.userService.siteApi.readUsersForSite(testData.newSite.id);
+ console.log(res.data.result);
+ expect(res.data.count).to.eq(0);
+ const userContext = await testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.BASIC_USER);
+ expect(res.data.result.map((site) => site.user.id)).to.not.contain(userContext.id);
+ });
+
it('Should be able to assign a site to a user', async () => {
- await assignSitesToUser(ContextDefinition.USER_CONTEXTS.BASIC_USER, [testData.newSite]);
+ await assignSitesToUser(ContextDefinition.USER_CONTEXTS.BASIC_USER, [testData.newSite.id]);
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);

MODIFY
test/api/client/SiteApi.ts
test/api/client/SiteApi.ts
@@ -42,6 +42,13 @@ export default class SiteApi extends CrudApi {
}, url);
}
+ public async unassignSitesToUser(userId: string, siteIds: string[]) {
+ const url = this.buildRestEndpointUrl(ServerRoute.REST_USER_SITES, { id: userId });
+ return super.update({
+ siteIDs: siteIds,
+ }, url);
+ }
+
public async readUsersForSite(siteId) {
return super.read({
SiteID: siteId

Added unassignSitesToUser endpoint + doc