How query nested array by time range mongodb with golang driver?

143 Views Asked by At

A have booking system with apoinments and need to get all booking objects with apoinments filtering with special time.

{
      "id": "6295001bef176110cb52076c",
      "companyID": "6294deeed16a4491020a6f3a",
      "createdAt": "2022-05-30T17:34:19.458Z",
      "updatedAt": "2022-05-31T12:23:58.805Z",
      "apoinments": [
        {
          "id": "62a349a1fe907103f8d33111",
          "startTime": "2022-06-10T12:00:00Z",
          "endTime": "2022-06-10T14:00:00Z",
          "createdAt": "2022-06-10T13:39:45.057Z",
          "companyID": "6294deeed16a4491020a6f3a",
          "bookingItemID": "6295001bef176110cb52076c",
        },
        {
          "id": "62a349c0fe907103f8d33112",
          "startTime": "2022-06-10T14:00:00Z",
          "endTime": "2022-06-10T16:00:00Z",
          "createdAt": "2022-06-10T13:40:16.927Z",
          "companyID": "6294deeed16a4491020a6f3a",
          "bookingItemID": "6295001bef176110cb52076c",
        }
      ],
      "title": "One",
    }

I need to get all booking objects with their apoinments with startTime and endTime with special range so i do:

func (companyRepository *companyRepositoryImpl) GetCompanyBookingItems(companyID string) (*model.CompanyBookingItems, error) {

    var existingCompany model.Company
    objectId, _ := primitive.ObjectIDFromHex(companyID)
    filter := bson.M{"_id": objectId}

    err := companyRepository.Connection.Collection("companies").FindOne(cntx, filter).Decode(&existingCompany)
    if err != nil {
        return nil, exception.ResourceNotFoundException("Company", "id", companyID)
    }

    switch hour := time.Now().Hour(); { // missing expression means "true"

    // dev
    case hour >= 8 && hour <= 23:
        // prod
        // case hour >= 3 && hour <= 18:
        // log.Println("hour", hour)
        startDate := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 11, 0, 0, 0, time.UTC)
        endDate := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()+1, 05, 0, 0, 0, time.UTC)

        bookingItems := *existingCompany.BookingItems

        for i := range bookingItems {

            bookingItem := bookingItems[i]

            var newApoinments []model.Apoinment

            for _, apoinment := range *bookingItem.Apoinments {

                if apoinment.StartTime.Local().After(startDate.Local()) && apoinment.EndTime.Local().Before(endDate.Local()) {
                    newApoinments = append(newApoinments, apoinment)
                    // log.Println("10 - 23", apoinment.ClientName)
                }
            }

            *bookingItems[i].Apoinments = newApoinments
        }

    // dev
    case hour >= 0 && hour <= 7:
        // prod
        // case hour >= 19 && hour <= 2:

        startDate := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-1, 11, 0, 0, 0, time.UTC)
        endDate := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 05, 0, 0, 0, time.UTC)
        // log.Println("start", startDate)
        // log.Println("end", endDate)

        bookingItems := *existingCompany.BookingItems

        for i := range bookingItems {

            bookingItem := bookingItems[i]

            var newApoinments []model.Apoinment

            for _, apoinment := range *bookingItem.Apoinments {
                if apoinment.StartTime.Local().After(startDate.Local()) && apoinment.EndTime.Local().Before(endDate.Local()) {
                    newApoinments = append(newApoinments, apoinment)
                    // log.Println("10 - 23", apoinment.ClientName)
                }
            }

            *bookingItems[i].Apoinments = newApoinments
        }
    }

    return &model.CompanyBookingItems{
        Data: *existingCompany.BookingItems,
    }, nil
}

but get error runtime error: invalid memory address or nil pointer dereference

How i can filter and mongodb return me only document with special ranged time apoinments of booking object?

0

There are 0 best solutions below