import { find } from 'lodash';
import moment from 'moment';
import {
  BaseModel, identifiedBy, field, identifier, belongsTo, computed, observable,
} from '../base';

import { TimeStore } from '../../flux/time';
@identifiedBy('purchase/product')
class Product extends BaseModel {
  @identifier uuid;
  @field name;
  @field price;
}

@identifiedBy('purchase')
export default class Purchase extends BaseModel {

  static URL = observable.shallowBox('');;

  @identifier identifier;
  @field product_instance_uuid;
  @field is_refunded;
  @field sales_tax;
  @field total;
  @field({ type: 'date' }) updated_at;
  @field({ type: 'date' }) purchased_at;
  @belongsTo({ model: Product }) product;

  @computed get isRefundable() {
    return !this.is_refunded &&
           moment(this.purchased_at).add(14, 'days').isAfter(TimeStore.getNow());
  }

  @computed get invoiceURL() {
    return Purchase.URL.get() + '/invoice/' + this.identifier;
  }

  refund() {
    return { item_uuid: this.product_instance_uuid };
  }

  onRefunded() {
    this.is_refunded = true;
  }

}

@@ -159,17 +159,54 @@
 /base';%0A
+import Courses from '../courses-map';
 %0A
-
 import %7B
@@ -724,16 +724,170 @@
 oduct;%0A%0A
+  @computed get course() %7B%0A    return find(Courses.array, c =%3E%0A      c.userStudentRecord && c.userStudentRecord.uuid == this.product_instance_uuid);%0A  %7D%0A%0A
   @compu
@@ -1214,24 +1214,24 @@
 efunded() %7B%0A
-
     this.is_
@@ -1247,15 +1247,82 @@
 = true;%0A
+    if (this.course) %7B%0A      Courses.delete(this.course.id);%0A    %7D%0A
   %7D%0A%0A%7D%0A

Remove course from courses list when refunded