All files / src/controllers/public user.controller.ts

69.57% Statements 16/23
0% Branches 0/10
0% Functions 0/3
66.67% Lines 14/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 1621x 1x 1x 1x 1x 1x 1x 1x 1x         1x                                                                                                                           1x                     1x                                           1x                                                                                 1x                        
import {authenticate} from '@loopback/authentication';
import {Constructor, intercept, service} from '@loopback/core';
import {ExtendOptions, createBaseControllerFactory, MixinOptions, BaseController} from '@logchain/controllers';
import {serialize} from '@logchain/interceptors';
import {Module, Constants, CONTENT_TYPE} from '@logchain/configs';
import {UserDto} from '@logchain/dtos';
import {User} from '@logchain/models';
import {UserService} from '@logchain/services';
import {get, oas, param, post, requestBody} from '@loopback/rest';
 
/**
 * Define the MixinOptions for create and updateByIdentifier
 */
const options: MixinOptions = {
  basePath: '/users',
  modelClass: User,
  modelDtoClass: UserDto,
  admin: false,
  schemaOptions: {
    create: {
      title: 'NewUser',
      exclude: [
        'sub',
        'company_id',
        'password',
        'change_password',
        'login_date',
        'status',
        'blockchain_status',
        'blockchain_certificate',
        'settings',
      ],
      authorize: {
        resource: Module.Admin.UserMgmt,
        scopes: [Constants.AccessScope.Create],
      },
    },
    updateByIdentifier: {
      exclude: [
        'sub',
        'company_id',
        'password',
        'change_password',
        'blockchain_status',
        'blockchain_certificate',
        'settings',
      ],
      authorize: {
        resource: Module.Admin.UserMgmt,
        scopes: [Constants.AccessScope.Update],
      },
    },
    findByIdentifier: {
      authorize: {
        resource: Module.Admin.UserMgmt,
        scopes: [Constants.AccessScope.Retrieve],
        exclude: ['settings'],
      },
    },
    find: {
      authorize: {
        resource: [Module.Admin.UserMgmt, Module.Compliance.FlowValidation].join(','),
        scopes: [Constants.AccessScope.Retrieve],
        exclude: ['settings'],
      },
    },
    deleteByIdentifier: {
      authorize: {
        resource: Module.Admin.UserMgmt,
        scopes: [Constants.AccessScope.Delete],
      },
    },
  },
};
 
const extendOptions: ExtendOptions = {
  isExtendCreate: true,
  isExtendRead: true,
  isExtendReadByIdentifier: true,
  isExtendUpdateByIdentifier: true,
  isExtendDeleteByIdentifier: true,
};
 
@oas.tags('User')
@intercept(serialize(UserDto))
@authenticate('aws-cognito')
export class UserController extends createBaseControllerFactory<User, Constructor<BaseController>>(
  BaseController,
  options,
  extendOptions,
) {
  constructor(
    @service(UserService)
    public selfService: UserService,
  ) {
    super();
  }
 
  /**
   * Update User settings by identifier
   */
  @post(`/${options.basePath}/logout`, {
    responses: {
      '200': {
        description: '',
      },
    },
  })
  async signout(
    @requestBody({
      content: {
        [CONTENT_TYPE.JSON]: {
          schema: {
            type: 'object',
            properties: {
              expired: {
                type: 'boolean',
                default: false,
              },
            },
          },
        },
      },
    })
    data: {
      sub: string;
    },
  ): Promise<void> {
    await this.selfService.signout(data);
  }
 
  @get(`/${options.basePath}/phone-by-email`, {
    responses: {
      '200': {
        description: 'Get phone number by email',
        content: {
          [CONTENT_TYPE.JSON]: {
            schema: {
              type: 'object',
              properties: {
                phone: {type: 'string'},
              },
            },
          },
        },
      },
    },
  })
  @authenticate.skip()
  async getPhoneByEmail(@param.query.string('email') email: string): Promise<{contact_number: string | null}> {
    if (!email) {
      throw new Error('Email is required');
    }
 
    const user = await this.selfService.getUserByEmail(email);
 
    return {
      contact_number: user?.contact_number ?? null,
    };
  }
}