Skip to content

Commit

Permalink
Merge pull request #192 from TEAM-SAMSION/PET-278
Browse files Browse the repository at this point in the history
PET-278 feat : 크리스마스 이벤트 투두 추가 생성 로직 추가
  • Loading branch information
tlarbals824 authored Dec 25, 2023
2 parents f4e939e + 80db66e commit 0364895
Showing 1 changed file with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import com.pawith.event.christmas.entity.ChristmasCategory;
import com.pawith.event.christmas.repository.ChristmasCategoryRepository;
import com.pawith.tododomain.entity.*;
import com.pawith.tododomain.repository.*;
import com.pawith.tododomain.repository.AssignRepository;
import com.pawith.tododomain.repository.CategoryRepository;
import com.pawith.tododomain.repository.RegisterRepository;
import com.pawith.tododomain.repository.TodoRepository;
import com.pawith.tododomain.service.RegisterQueryService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -81,6 +85,46 @@ public void addNewRegisterAtChristmasEventTodo(Long todoTeamId, Long userId){
});
}

@Scheduled(cron = "0 0 0 26 12 *")
public void addNewTodoAtChristmasEventCategory(){
final List<ChristmasCategory> allChristmasCategory = christmasCategoryRepository.findAll();
List<Todo> todoList = new ArrayList<>();
List<Assign> assignList = new ArrayList<>();
allChristmasCategory.forEach(christmasCategory ->{
final Long categoryId = christmasCategory.getCategoryId();
categoryRepository.findById(categoryId).ifPresent(category -> {
final List<LocalDate> eventDateList = new ArrayList<>(EVENT_START_DATE.datesUntil(EVENT_END_DATE).toList());
final TodoTeam todoTeam = category.getTodoTeam();
final List<Register> allRegisters = registerRepository.findAllByTodoTeamId(todoTeam.getId());
todoRepository.findTodoListByCreatorIdAndTodoTeamIdQuery(EVENT_CREATOR_ID, todoTeam.getId())
.forEach(todo -> {
final LocalDate scheduledDate = todo.getScheduledDate();
eventDateList.remove(scheduledDate);
});

eventDateList.forEach(eventDate -> {
final Todo todo = Todo.builder()
.category(category)
.creatorId(EVENT_CREATOR_ID)
.description(EVENT_TODO_DESCRIPTION)
.scheduledDate(eventDate)
.build();
todoList.add(todo);
List<Assign> allAssign = allRegisters.stream()
.filter(Register::isRegistered)
.map(register -> Assign.builder()
.todo(todo)
.register(register)
.build())
.toList();
assignList.addAll(allAssign);
});
});
});
todoRepository.saveAll(todoList);
assignRepository.saveAll(assignList);
}

public Boolean isEventDay(){
final LocalDate now = LocalDate.now();
return now.isAfter(EVENT_START_DATE) && now.isBefore(EVENT_END_DATE);
Expand Down

0 comments on commit 0364895

Please sign in to comment.