Skip to content

Commit

Permalink
feat(scheduler-service): SFO-102 add permissions in calendar (#42)
Browse files Browse the repository at this point in the history
* feat(scheduler-service): add permissions in calendar
  • Loading branch information
IshuSF authored Jul 6, 2020
1 parent 35e9ecd commit 12d1868
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
43 changes: 32 additions & 11 deletions services/scheduler-service/src/controllers/calendar.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export class CalendarController {
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
@authorize([PermissionKey.CreateCalendar])
@authorize([
PermissionKey.CreateCalendar
])
@post(basePath, {
responses: {
[STATUS_CODE.OK]: {
Expand Down Expand Up @@ -94,10 +96,12 @@ export class CalendarController {
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
@authorize([PermissionKey.CreateCalendar])
@authorize([
PermissionKey.CreateCalendar
])
@post('/calendars/calendarSubscription', {
responses: {
'200': {
[STATUS_CODE.OK]: {
description: calendarModelInstance,
content: {'application/json': {schema: getModelSchemaRef(CalendarDTO)}},
},
Expand All @@ -123,26 +127,41 @@ export class CalendarController {

delete calendarDTO.subscription;
let response = await this.calendarService.createCalendar(calendarDTO);

let identifierType = this.schdulerConfig?.identifierMappedTo;
if (!identifierType) {
identifierType = IdentifierType.Id;
}

let subscriptionIdentifier;
if(subscription.identifier){
subscriptionIdentifier = subscription.identifier;
}
else{
subscriptionIdentifier = this.currentUser[identifierType];
}

const subscriptionList = await this.subscriptionRepository.find({
where: {
identifier: this.currentUser[identifierType],
},
});
const where = {
and: [
{identifier: subscriptionIdentifier},
{isPrimary: true}
]
};

const subscriptionList = await this.subscriptionRepository.find({where});
if (subscriptionList.length > 0) {
subscription.isPrimary = false;
} else {
subscription.isPrimary = true;
}

if (response.id) {
subscription.calendarId = response.id;
subscription.identifier = calendarDTO.identifier;
if(subscriptionIdentifier){
subscription.identifier = subscriptionIdentifier;
}
else{
throw new HttpErrors.NotFound(ErrorKeys.SubscriptionIdentifierNotExist);
}
subscription.accessRole = AccessRoleType.Owner;
const subscriptionResponse = await this.subscriptionRepository.create(
subscription,
Expand Down Expand Up @@ -273,7 +292,9 @@ export class CalendarController {
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
@authorize([PermissionKey.UpdateCalendar])
@authorize([
PermissionKey.UpdateCalendar
])
@put(`${basePath}/{id}`, {
responses: {
[STATUS_CODE.NO_CONTENT]: {
Expand Down
1 change: 1 addition & 0 deletions services/scheduler-service/src/models/enums/error-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const enum ErrorKeys {
PrimaryCalendarNotExist = 'Primary Calendar Not Exist',
EventNotExist = 'Event Not Exist',
SubscriptionNotExist = 'Subscription Not exist',
SubscriptionIdentifierNotExist = 'Subscription Identifier Not exist',
IdNotExist = 'Id Not Exist',

//unprocessable
Expand Down

0 comments on commit 12d1868

Please sign in to comment.