/**
  * Create a member property
  *
  * <b>Request Type: </b>POST<br/>
  * <b>Request Endpoint: </b>http://{{server-domain}}/api/common/member-propertys<br/>
  * <b>Content-type: </b>application/json<br/>
  * <b>Summary: </b>This api is for create a new member property.<br/>
  *
  * <b>Request Example:</b>
  * <pre>
  *{
  *   "order": 7,
  *   "name": "name",
  *  "type": "input",
  *   "defaultValue": "Devin Jin",
  *   "isRequired": true,
  *   "isUnique": true,
  *   "isVisible": true,
  *   "isDefault": true
  * }
  */
 public function actionCreate()
 {
     $accountId = $this->getAccountId();
     //can not exceed 100 properties for an account
     $propertiesCount = MemberProperty::count(['accountId' => $accountId]);
     if ($propertiesCount >= MemberProperty::MAX_COUNT) {
         throw new BadRequestHttpException(Yii::t('member', 'property_max_error'));
     }
     $params = $this->getParams();
     // get the max order property
     $condition = ['accountId' => $accountId, 'isDeleted' => MemberProperty::NOT_DELETED];
     $orderProperty = MemberProperty::find()->where($condition)->orderBy(['order' => SORT_DESC])->one();
     $order = 1;
     if (!empty($orderProperty)) {
         $order = $orderProperty['order'] + 1;
     }
     $property = new MemberProperty();
     $property->load($params, '');
     $property->order = $order;
     $property->accountId = $accountId;
     if ($property->save()) {
         //update all the members of the account
         Member::updateAll(['$push' => ['properties' => ['id' => $property->_id, 'name' => $property->name, 'value' => $property->defaultValue]]], ['accountId' => $accountId]);
         return $property;
     } else {
         if ($property->hasErrors()) {
             throw new BadRequestHttpException(Json::encode($property->errors));
         }
     }
     throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
 }
Example #2
0
 public function validateProperties($attribute)
 {
     if ($attribute != 'properties' || $this->name != self::NAME_PERFECT_INFO) {
         return true;
     }
     $properties = $this->{$attribute};
     if (empty($properties)) {
         throw new InvalidParameterException(['rulePropertiesRequired' => \Yii::t('common', 'required_filed')]);
     }
     $visablePropertyCount = MemberProperty::count(['_id' => ['$in' => $properties], 'isVisible' => true]);
     if ($visablePropertyCount != count($properties)) {
         throw new InvalidParameterException(['rulePropertiesRequired' => \Yii::t('member', 'rule_property_error')]);
     }
 }