Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pet-278 fix : 이벤트 기간 연장에 따른 수정 사항 반영 #193

Merged
merged 7 commits into from
Dec 26, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Component
public class ChristmasEventCloseHandler extends AbstractBatchSchedulingHandler<ChristmasCategory> {
private static final Integer BATCH_SIZE = 100;
private static final String CRON_EXPRESSION = "0 0 0 26 12 *";
private static final String CRON_EXPRESSION = "0 0 0 2 1 *";

private final ChristmasCategoryRepository christmasCategoryRepository;
private final CategoryRepository categoryRepository;
Expand Down
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 All @@ -18,12 +22,8 @@
public class ChristmasEventService {
private static final String EVENT_CATEGORY_NAME = "크리스마스";
private static final String EVENT_TODO_DESCRIPTION = "포잇스마스 이벤트 참여하기";
private static final List<LocalDate> EVENT_TODO_SCHEDULED_DATE_LIST = List.of(
LocalDate.of(2023, 12, 23),
LocalDate.of(2023, 12, 24),
LocalDate.of(2023, 12, 25)
);

private static final LocalDate EVENT_START_DATE = LocalDate.of(2023, 12, 23);
private static final LocalDate EVENT_END_DATE = LocalDate.of(2024, 1, 2);

private static final Long EVENT_CREATOR_ID = -1L;
private final CategoryRepository categoryRepository;
Expand All @@ -47,7 +47,9 @@ public void publishEvent(TodoTeam todoTeam){
final List<Register> allRegisters = registerRepository.findAllByTodoTeamId(todoTeam.getId());
final List<Todo> eventTodoList = new ArrayList<>();
final List<Assign> eventAssignList = new ArrayList<>();
EVENT_TODO_SCHEDULED_DATE_LIST

final List<LocalDate> eventDateList = EVENT_START_DATE.datesUntil(EVENT_END_DATE).toList();
eventDateList
.forEach(eventDate -> {
final Todo todo = Todo.builder()
.category(saved)
Expand Down Expand Up @@ -83,8 +85,49 @@ 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(){
return EVENT_TODO_SCHEDULED_DATE_LIST.contains(LocalDate.now());
final LocalDate now = LocalDate.now();
return now.isAfter(EVENT_START_DATE) && now.isBefore(EVENT_END_DATE);
}

}
Loading